[clang] 1425fa9 - [clang][bytecode] Check FromPtr in BitCastPtr (#117142)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 21 04:21:04 PST 2024


Author: Timm Baeder
Date: 2024-11-21T13:21:00+01:00
New Revision: 1425fa915dd4815e10b97373380e049db3a01cd3

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

LOG: [clang][bytecode] Check FromPtr in BitCastPtr (#117142)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/test/AST/ByteCode/builtin-bit-cast.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 1f3134e1cd1559..47dcfca79f7356 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3084,6 +3084,9 @@ inline bool BitCastPtr(InterpState &S, CodePtr OpPC) {
   const Pointer &FromPtr = S.Stk.pop<Pointer>();
   Pointer &ToPtr = S.Stk.peek<Pointer>();
 
+  if (!CheckLoad(S, OpPC, FromPtr))
+    return false;
+
   if (!DoBitCastPtr(S, OpPC, FromPtr, ToPtr))
     return false;
 

diff  --git a/clang/test/AST/ByteCode/builtin-bit-cast.cpp b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
index 3c6cd0aa36e7fe..0fecde59cd57ce 100644
--- a/clang/test/AST/ByteCode/builtin-bit-cast.cpp
+++ b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
@@ -156,6 +156,17 @@ namespace ToPtr {
                                               // both-note {{bit_cast to a pointer type is not allowed in a constant expression}}
 }
 
+namespace Invalid {
+  struct S {
+    int a;
+  };
+  constexpr S s = S{1/0}; // both-error {{must be initialized by a constant expression}} \
+                          // both-note {{division by zero}} \
+                          // both-note {{declared here}}
+  constexpr S s2 = __builtin_bit_cast(S, s); // both-error {{must be initialized by a constant expression}} \
+                                             // both-note {{initializer of 's' is not a constant expression}}
+}
+
 namespace NullPtr {
   constexpr nullptr_t N = __builtin_bit_cast(nullptr_t, (intptr_t)1u);
   static_assert(N == nullptr);


        


More information about the cfe-commits mailing list