Skip to content

Commit b6217f6

Browse files
authored
[clang][bytecode] Fix bitcasting from null pointers (#116999)
1 parent 71bbafb commit b6217f6

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,13 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
254254
}
255255

256256
assert(P.isInitialized());
257-
// nullptr_t is a PT_Ptr for us, but it's still not std::is_pointer_v.
258-
if (T == PT_Ptr)
259-
assert(false && "Implement casting to pointer types");
257+
if (T == PT_Ptr) {
258+
assert(P.getType()->isNullPtrType());
259+
// Clang treats nullptr_t has having NO bits in its value
260+
// representation. So, we accept it here and leave its bits
261+
// uninitialized.
262+
return true;
263+
}
260264

261265
auto Buff =
262266
std::make_unique<std::byte[]>(ObjectReprChars.getQuantity());

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,14 @@ namespace simple {
133133

134134
/// This works in GCC and in the bytecode interpreter, but the current interpreter
135135
/// diagnoses it.
136+
/// FIXME: Should also be rejected in the bytecode interpreter.
136137
static_assert(__builtin_bit_cast(intptr_t, nullptr) == 0); // ref-error {{not an integral constant expression}} \
137138
// ref-note {{indeterminate value can only initialize an object}}
139+
140+
constexpr int test_from_nullptr_pass = (__builtin_bit_cast(unsigned char[sizeof(nullptr)], nullptr), 0);
141+
constexpr unsigned char NPData[sizeof(nullptr)] = {1,2,3,4};
142+
constexpr nullptr_t NP = __builtin_bit_cast(nullptr_t, NPData);
143+
static_assert(NP == nullptr);
138144
}
139145

140146
namespace Fail {

0 commit comments

Comments
 (0)