[clang] 32a9f63 - [clang][bytecode] Fix some problems with ptr-to-int casts (#193988)

via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 25 23:04:55 PDT 2026


Author: Timm Baeder
Date: 2026-04-26T08:04:50+02:00
New Revision: 32a9f63bb2533a9d564f8d114b139e821aa52415

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

LOG: [clang][bytecode] Fix some problems with ptr-to-int casts (#193988)

1) When doing integral casts on a pointer-casted-to-integral, check the
bitwidth we're casting _to_, not the one we're casting _from_.
2) When the pointer we're casting to an integral is a dummy pointer,
don't forget to check the bitwidth.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.cpp
    clang/lib/AST/ByteCode/Interp.h
    clang/test/Sema/static-init.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 249a282482749..21a75b8fb4d45 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -2234,13 +2234,15 @@ bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC,
   if (Ptr.isIntegralPointer())
     return true;
 
-  if (Ptr.isDummy())
+  if (Ptr.isDummy()) {
+    if (!CheckIntegralAddressCast(S, OpPC, BitWidth))
+      return false;
     return Ptr.getIndex() == 0;
+  }
 
   if (!Ptr.isZero()) {
     // Only allow based lvalue casts if they are lossless.
-    if (S.getASTContext().getTargetInfo().getPointerWidth(LangAS::Default) !=
-        BitWidth)
+    if (!CheckIntegralAddressCast(S, OpPC, BitWidth))
       return Invalid(S, OpPC);
   }
   return true;

diff  --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 293ef3c2a639c..87ee350e9c5b7 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2707,7 +2707,7 @@ template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
   if constexpr (isIntegralOrPointer<T>()) {
     if (In.getKind() != IntegralKind::Number &&
         In.getKind() != IntegralKind::AddrLabelDiff) {
-      if (!CheckIntegralAddressCast(S, OpPC, In.bitWidth()))
+      if (!CheckIntegralAddressCast(S, OpPC, U::bitWidth()))
         return Invalid(S, OpPC);
     } else if (In.getKind() == IntegralKind::AddrLabelDiff) {
       // Allow casts of address-of-label 
diff erences if they are no-ops

diff  --git a/clang/test/Sema/static-init.c b/clang/test/Sema/static-init.c
index 5333b522bde16..ecd7b5c78d0d5 100644
--- a/clang/test/Sema/static-init.c
+++ b/clang/test/Sema/static-init.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-pointer-to-int-cast -Wno-bool-conversion %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-pointer-to-int-cast -Wno-bool-conversion %s -fexperimental-new-constant-interpreter
 
 typedef __typeof((int*) 0 - (int*) 0) intptr_t;
 


        


More information about the cfe-commits mailing list