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

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


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

None

>From ea7847f4f414e052e0349fe503a2001b930bf22f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 21 Nov 2024 12:20:12 +0100
Subject: [PATCH] [clang][bytecode] Check FromPtr in BitCasttPtr

---
 clang/lib/AST/ByteCode/Interp.h              |  3 +++
 clang/test/AST/ByteCode/builtin-bit-cast.cpp | 11 +++++++++++
 2 files changed, 14 insertions(+)

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