Skip to content

Commit 7c06ab9

Browse files
committed
[clang][bytecode] Check memcmp builtin for one-past-the-end pointers
We can't read from those and will run into an assertion sooner or later. Fixes #170031
1 parent 8e6fb0e commit 7c06ab9

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,10 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
19211921
if (PtrA.isDummy() || PtrB.isDummy())
19221922
return false;
19231923

1924+
if (!CheckRange(S, OpPC, PtrA, AK_Read) ||
1925+
!CheckRange(S, OpPC, PtrB, AK_Read))
1926+
return false;
1927+
19241928
// Now, read both pointers to a buffer and compare those.
19251929
BitcastBuffer BufferA(
19261930
Bits(ASTCtx.getTypeSize(ElemTypeA) * PtrA.getNumElems()));

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,13 @@ namespace Memcmp {
15451545

15461546
int unknown;
15471547
void foo(void) { unknown *= __builtin_memcmp(0, 0, 2); }
1548+
1549+
constexpr int onepasttheend(char a) {
1550+
__builtin_memcmp(&a, &a + 1, 1); // both-note {{read of dereferenced one-past-the-end pointer}}
1551+
return 1;
1552+
}
1553+
static_assert(onepasttheend(10)); // both-error {{not an integral constant expression}} \
1554+
// both-note {{in call to}}
15481555
}
15491556

15501557
namespace Memchr {

0 commit comments

Comments
 (0)