Skip to content

[clang][bytecode] Check FromPtr in BitCastPtr #117142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions clang/test/AST/ByteCode/builtin-bit-cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading