Skip to content

Commit bb09cc4

Browse files
authored
[clang][bytecode] Fix a crash in __builtin_object_size() (llvm#160519)
The previous `ByteOffset` computation only makes sense if `Ptr` points into an array.
1 parent ff394cd commit bb09cc4

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,10 +2314,14 @@ static bool interp__builtin_object_size(InterpState &S, CodePtr OpPC,
23142314
if (Ptr.isBaseClass())
23152315
ByteOffset = computePointerOffset(ASTCtx, Ptr.getBase()) -
23162316
computePointerOffset(ASTCtx, Ptr);
2317-
else
2318-
ByteOffset =
2319-
computePointerOffset(ASTCtx, Ptr) -
2320-
computePointerOffset(ASTCtx, Ptr.expand().atIndex(0).narrow());
2317+
else {
2318+
if (Ptr.inArray())
2319+
ByteOffset =
2320+
computePointerOffset(ASTCtx, Ptr) -
2321+
computePointerOffset(ASTCtx, Ptr.expand().atIndex(0).narrow());
2322+
else
2323+
ByteOffset = 0;
2324+
}
23212325
} else
23222326
ByteOffset = computePointerOffset(ASTCtx, Ptr);
23232327

clang/test/AST/ByteCode/builtin-object-size.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ static_assert(__builtin_object_size(&arrf, 0) == (sizeof(float)*2), "");
1717
static_assert(__builtin_object_size(&arrf[1], 0) == sizeof(float), "");
1818
static_assert(__builtin_object_size(&arrf[2], 0) == 0, "");
1919

20-
20+
constexpr struct { int a; int b; } F{};
21+
static_assert(__builtin_object_size(&F.a, 3) == sizeof(int));
2122

2223
struct S {
2324
int a;

0 commit comments

Comments
 (0)