[clang] [clang][bytecode] Fix bitcast from nullptr_t to array (PR #237165)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 30 03:27:44 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/237165
First, we need to make sure that initialize() on an arrayroot pointer does not initialize the first element of the array; second, it is possible for such an array to not have an associated FieldDecl, as is the case in the materialized temporary array in the attached reproducer.
>From 0fc6e811ef3717fb7ea2fe0046b6939c4c64d64c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 30 Oct 2026 11:21:18 +0100
Subject: [PATCH] [clang][bytecode] Fix bitcast from nullptr_t to array
First, we need to make sure that initialize() on an arrayroot pointer
does not initialize the first element of the array; second, it is
possible for such an array to not have an associated FieldDecl, as is
the case in the materialized temporary array in the attached reproducer.
---
clang/lib/AST/ByteCode/Compiler.cpp | 6 ++++--
clang/lib/AST/ByteCode/EvaluationResult.cpp | 3 ++-
clang/lib/AST/ByteCode/Pointer.cpp | 2 +-
clang/lib/AST/ByteCode/Pointer.h | 2 +-
clang/test/AST/ByteCode/builtin-bit-cast.cpp | 8 ++++++++
5 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 164dd155debd42..3cd3297ed18b00 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1310,7 +1310,9 @@ bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
return false;
if (!this->emitInitElem(SubExprT, 0, SubExpr))
return false;
- return this->visitArrayElemInit(1, SubExpr, SubExprT);
+ if (!this->visitArrayElemInit(1, SubExpr, SubExprT))
+ return false;
+ return this->emitFinishInit(E);
}
template <class Emitter>
@@ -1884,7 +1886,7 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
return this->emitPopPtr(E);
}
}
- return true;
+ return this->emitFinishInit(E);
}
template <class Emitter>
diff --git a/clang/lib/AST/ByteCode/EvaluationResult.cpp b/clang/lib/AST/ByteCode/EvaluationResult.cpp
index 70f64568483fdd..d6dbd8c80f32f7 100644
--- a/clang/lib/AST/ByteCode/EvaluationResult.cpp
+++ b/clang/lib/AST/ByteCode/EvaluationResult.cpp
@@ -62,7 +62,8 @@ static bool CheckArrayInitialized(InterpState &S, SourceLocation Loc,
if (BaseDesc->isPrimitiveArray()) {
if (BasePtr.allElementsInitialized())
return true;
- DiagnoseUninitializedSubobject(S, Loc, BasePtr.getField());
+ if (const FieldDecl *F = BasePtr.getField())
+ DiagnoseUninitializedSubobject(S, Loc, F);
return false;
}
const Descriptor *ElemDesc = BaseDesc->ElemDesc;
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 3890f2ed2f91de..35ffa4e68373a8 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -814,7 +814,7 @@ void PtrView::initialize() const {
const Descriptor *Desc = getFieldDesc();
assert(Desc);
- if (Desc->isPrimitiveArray()) {
+ if (Offset != Base && Desc->isPrimitiveArray()) {
if (Desc->getNumElems() != 0)
initializeElement(getIndex());
return;
diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index 0fc540cdd88586..8cedb529b73a02 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -315,7 +315,7 @@ struct PtrView {
assert(Pointee && "Cannot check if null pointer was initialized");
const Descriptor *Desc = getFieldDesc();
assert(Desc);
- if (Desc->isPrimitiveArray())
+ if (Offset != Base && Desc->isPrimitiveArray())
return isElementInitialized(getIndex());
if (Base == 0)
diff --git a/clang/test/AST/ByteCode/builtin-bit-cast.cpp b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
index 2aaf84f9130c79..8588b4217a4d9a 100644
--- a/clang/test/AST/ByteCode/builtin-bit-cast.cpp
+++ b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
@@ -616,3 +616,11 @@ namespace StringInDoBitCast {
static_assert(foo<b32>(0x42), ""); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
}
+
+namespace nullptrToArray {
+ typedef decltype(__builtin_bit_cast(unsigned char[sizeof(nullptr)], nullptr)) footype;
+ static_assert(__builtin_bit_cast(footype, nullptr) == nullptr); // both-error {{failed due to requirement}}
+ static_assert(__builtin_bit_cast(footype, nullptr)[0] == 0); // both-error {{not an integral constant expression}} \
+ // both-note {{read of uninitialized object}} \
+ // both-note {{temporary created here}}
+}
More information about the cfe-commits
mailing list