Skip to content

Commit df61a9a

Browse files
committed
Fix clad::array copy-assignment over-reading a shorter source.
array<T>::operator=(const array<T>&) only reallocated when the destination was smaller than the source, so a larger destination kept its old m_size and then copied that many elements from the shorter source -- a heap over-read Valgrind flags in Jacobian/Jacobian.C and Jacobian/testUtility.C once the clang false positives are suppressed. Adopt the source size before copying so the loop is bounded by the source. The buffer may exceed m_size after a shrink, which is harmless: m_size bounds every access and the destructor frees the whole buffer.
1 parent 5019711 commit df61a9a

3 files changed

Lines changed: 4 additions & 5 deletions

File tree

include/clad/Differentiator/Array.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ template <typename T> class array {
9898
delete[] m_arr;
9999
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
100100
m_arr = new T[arr.m_size];
101-
m_size = arr.m_size;
102101
}
102+
// Adopt the source size before delegating to operator=(T*): otherwise a
103+
// larger destination keeps its old m_size and that overload reads
104+
// arr.m_arr past its end (heap over-read on shrinking assignment).
105+
m_size = arr.m_size;
103106
(*this) = arr.m_arr;
104107
return *this;
105108
}

test/Jacobian/Jacobian.C

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %cladclang %s -I%S/../../include -oJacobian.out 2>&1 | %filecheck %s
22
// RUN: ./Jacobian.out | %filecheck_exec %s
3-
// FIXME: real clad::array copy-assignment over-read; drop when the fix lands.
4-
// XFAIL: valgrind
53

64
#include "clad/Differentiator/Differentiator.h"
75
#include <cmath>

test/Jacobian/testUtility.C

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %cladclang %s -I%S/../../include -otestUtility.out 2>&1 | %filecheck %s
22
// RUN: ./testUtility.out | %filecheck_exec %s
3-
// FIXME: real clad::array copy-assignment over-read; drop when the fix lands.
4-
// XFAIL: valgrind
53

64
#include "clad/Differentiator/Differentiator.h"
75

0 commit comments

Comments
 (0)