Skip to content

Commit 7669676

Browse files
committed
[clang][bytecode] Fix a crash in __builtin_object_size()
The previous BytOffset computation only makes sense if Ptr points into an array.
1 parent 094d313 commit 7669676

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
@@ -2449,10 +2449,14 @@ static bool interp__builtin_object_size(InterpState &S, CodePtr OpPC,
24492449
if (Ptr.isBaseClass())
24502450
ByteOffset = computePointerOffset(ASTCtx, Ptr.getBase()) -
24512451
computePointerOffset(ASTCtx, Ptr);
2452-
else
2453-
ByteOffset =
2454-
computePointerOffset(ASTCtx, Ptr) -
2455-
computePointerOffset(ASTCtx, Ptr.expand().atIndex(0).narrow());
2452+
else {
2453+
if (Ptr.inArray())
2454+
ByteOffset =
2455+
computePointerOffset(ASTCtx, Ptr) -
2456+
computePointerOffset(ASTCtx, Ptr.expand().atIndex(0).narrow());
2457+
else
2458+
ByteOffset = 0;
2459+
}
24562460
} else
24572461
ByteOffset = computePointerOffset(ASTCtx, Ptr);
24582462

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)