Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,20 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
return false;
}

// Check for overlapping memory regions.
if (!Move && SrcPtr.block() == DestPtr.block()) {
unsigned SrcIndex = SrcPtr.getIndex() * SrcPtr.elemSize();
unsigned DstIndex = DestPtr.getIndex() * DestPtr.elemSize();
unsigned N = Size.getZExtValue();

if ((SrcIndex <= DstIndex && (SrcIndex + N) > DstIndex) ||
(DstIndex <= SrcIndex && (DstIndex + N) > SrcIndex)) {
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_overlap)
<< /*IsWChar=*/false;
return false;
}
}

// As a last resort, reject dummy pointers.
if (DestPtr.isDummy() || SrcPtr.isDummy())
return false;
Expand Down
15 changes: 15 additions & 0 deletions clang/test/AST/ByteCode/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,19 @@ namespace BuiltinMemcpy {
}
static_assert(memcpyTypeRem() == 12); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}

template<typename T>
constexpr T result(T (&arr)[4]) {
return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
}

constexpr int test_memcpy(int a, int b, int n) {
int arr[4] = {1, 2, 3, 4};
__builtin_memcpy(arr + a, arr + b, n); // both-note {{overlapping memory regions}}
return result(arr);
}

static_assert(test_memcpy(1, 2, sizeof(int)) == 1334);
static_assert(test_memcpy(0, 1, sizeof(int) * 2) == 2334); // both-error {{not an integral constant expression}} \
// both-note {{in call}}
}
Loading