[clang] bb09cc4 - [clang][bytecode] Fix a crash in __builtin_object_size() (#160519)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 6 23:21:25 PDT 2025


Author: Timm Baeder
Date: 2025-10-07T08:21:21+02:00
New Revision: bb09cc41cf981001c178d956174f59e6bd324a2d

URL: https://github.com/llvm/llvm-project/commit/bb09cc41cf981001c178d956174f59e6bd324a2d
DIFF: https://github.com/llvm/llvm-project/commit/bb09cc41cf981001c178d956174f59e6bd324a2d.diff

LOG: [clang][bytecode] Fix a crash in __builtin_object_size() (#160519)

The previous `ByteOffset` computation only makes sense if `Ptr` points
into an array.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/InterpBuiltin.cpp
    clang/test/AST/ByteCode/builtin-object-size.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 6af7ef32e69aa..1eea813b8c556 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -2314,10 +2314,14 @@ static bool interp__builtin_object_size(InterpState &S, CodePtr OpPC,
     if (Ptr.isBaseClass())
       ByteOffset = computePointerOffset(ASTCtx, Ptr.getBase()) -
                    computePointerOffset(ASTCtx, Ptr);
-    else
-      ByteOffset =
-          computePointerOffset(ASTCtx, Ptr) -
-          computePointerOffset(ASTCtx, Ptr.expand().atIndex(0).narrow());
+    else {
+      if (Ptr.inArray())
+        ByteOffset =
+            computePointerOffset(ASTCtx, Ptr) -
+            computePointerOffset(ASTCtx, Ptr.expand().atIndex(0).narrow());
+      else
+        ByteOffset = 0;
+    }
   } else
     ByteOffset = computePointerOffset(ASTCtx, Ptr);
 

diff  --git a/clang/test/AST/ByteCode/builtin-object-size.cpp b/clang/test/AST/ByteCode/builtin-object-size.cpp
index 6f4ef54bcbafa..e4433ea700ccb 100644
--- a/clang/test/AST/ByteCode/builtin-object-size.cpp
+++ b/clang/test/AST/ByteCode/builtin-object-size.cpp
@@ -17,7 +17,8 @@ static_assert(__builtin_object_size(&arrf, 0) == (sizeof(float)*2), "");
 static_assert(__builtin_object_size(&arrf[1], 0) == sizeof(float), "");
 static_assert(__builtin_object_size(&arrf[2], 0) == 0, "");
 
-
+constexpr struct { int a; int b; } F{};
+static_assert(__builtin_object_size(&F.a, 3) == sizeof(int));
 
 struct S {
   int a;


        


More information about the cfe-commits mailing list