[clang] [clang][bytecode] Check FromPtr in BitCasttPtr (PR #117142)

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/117142.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Interp.h (+3) 
- (modified) clang/test/AST/ByteCode/builtin-bit-cast.cpp (+11) 


``````````diff
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);

``````````

</details>


https://github.com/llvm/llvm-project/pull/117142


More information about the cfe-commits mailing list