Skip to content

Commit 004e462

Browse files
authored
[clang][bytecode] Fix unknown size arrays crash in clang bytecode (#160015)
Fixes #153948 Signed-off-by: Osama Abdelkader <[email protected]>
1 parent fcebe6b commit 004e462

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ bool Context::evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result) {
245245
if (!FieldDesc->isPrimitiveArray())
246246
return false;
247247

248+
if (Ptr.isDummy() || Ptr.isUnknownSizeArray())
249+
return false;
250+
248251
unsigned N = Ptr.getNumElems();
249252
if (Ptr.elemSize() == 1) {
250253
Result = strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), N);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter %s -verify
2+
// RUN: %clang_cc1 -std=c++20 %s -verify=ref
3+
4+
// expected-no-diagnostics
5+
// ref-no-diagnostics
6+
7+
/// Test that __builtin_strlen() on external/unknown declarations doesn't crash the bytecode interpreter.
8+
extern const char s[];
9+
void foo(char *x)
10+
{
11+
unsigned long len = __builtin_strlen(s);
12+
__builtin_strcpy(x, s);
13+
}

0 commit comments

Comments
 (0)