Skip to content

Commit 3923adf

Browse files
authored
[clang][bytecode] Guard strcmp against differing element types (#154777)
This can happen when casts are involved. Fixes #154006
1 parent dacabc1 commit 3923adf

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,19 @@ static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC,
205205

206206
if (A.isDummy() || B.isDummy())
207207
return false;
208+
if (!A.isBlockPointer() || !B.isBlockPointer())
209+
return false;
208210

209211
bool IsWide = ID == Builtin::BIwcscmp || ID == Builtin::BIwcsncmp ||
210212
ID == Builtin::BI__builtin_wcscmp ||
211213
ID == Builtin::BI__builtin_wcsncmp;
212214
assert(A.getFieldDesc()->isPrimitiveArray());
213215
assert(B.getFieldDesc()->isPrimitiveArray());
214216

215-
assert(getElemType(A).getTypePtr() == getElemType(B).getTypePtr());
217+
// Different element types shouldn't happen, but with casts they can.
218+
if (!S.getASTContext().hasSameUnqualifiedType(getElemType(A), getElemType(B)))
219+
return false;
220+
216221
PrimType ElemT = *S.getContext().classify(getElemType(A));
217222

218223
auto returnResult = [&](int V) -> bool {

clang/test/AST/ByteCode/c.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,12 @@ static void *FooTable[1] = {
338338
}
339339
};
340340

341+
int strcmp(const char *, const char *); // all-note {{passing argument to parameter here}}
342+
#define S "\x01\x02\x03\x04\x05\x06\x07\x08"
343+
const char _str[] = {S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7]};
344+
const unsigned char _str2[] = {S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7]};
345+
const int compared = strcmp(_str, (const char *)_str2); // all-error {{initializer element is not a compile-time constant}}
346+
347+
348+
const int compared2 = strcmp(strcmp, _str); // all-warning {{incompatible pointer types}} \
349+
// all-error {{initializer element is not a compile-time constant}}

0 commit comments

Comments
 (0)