Skip to content

Commit 1425fa9

Browse files
authored
[clang][bytecode] Check FromPtr in BitCastPtr (#117142)
1 parent af641ff commit 1425fa9

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,6 +3084,9 @@ inline bool BitCastPtr(InterpState &S, CodePtr OpPC) {
30843084
const Pointer &FromPtr = S.Stk.pop<Pointer>();
30853085
Pointer &ToPtr = S.Stk.peek<Pointer>();
30863086

3087+
if (!CheckLoad(S, OpPC, FromPtr))
3088+
return false;
3089+
30873090
if (!DoBitCastPtr(S, OpPC, FromPtr, ToPtr))
30883091
return false;
30893092

clang/test/AST/ByteCode/builtin-bit-cast.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ namespace ToPtr {
156156
// both-note {{bit_cast to a pointer type is not allowed in a constant expression}}
157157
}
158158

159+
namespace Invalid {
160+
struct S {
161+
int a;
162+
};
163+
constexpr S s = S{1/0}; // both-error {{must be initialized by a constant expression}} \
164+
// both-note {{division by zero}} \
165+
// both-note {{declared here}}
166+
constexpr S s2 = __builtin_bit_cast(S, s); // both-error {{must be initialized by a constant expression}} \
167+
// both-note {{initializer of 's' is not a constant expression}}
168+
}
169+
159170
namespace NullPtr {
160171
constexpr nullptr_t N = __builtin_bit_cast(nullptr_t, (intptr_t)1u);
161172
static_assert(N == nullptr);

0 commit comments

Comments
 (0)