Skip to content

[clang][bytecode] Diagnose dereferencing a null pointer #149330

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 2 commits into from
Jul 19, 2025
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/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6375,6 +6375,9 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
if (!this->visit(SubExpr))
return false;

if (!this->emitCheckNull(E))
return false;

if (classifyPrim(SubExpr) == PT_Ptr)
return this->emitNarrowPtr(E);
return true;
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,16 @@ inline bool Dump(InterpState &S, CodePtr OpPC) {
return true;
}

inline bool CheckNull(InterpState &S, CodePtr OpPC) {
const auto &Ptr = S.Stk.peek<Pointer>();
if (Ptr.isZero()) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_dereferencing_null);
return S.noteUndefinedBehavior();
}
return true;
}

inline bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl,
const Pointer &Ptr) {
Pointer Base = Ptr;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ def CheckNewTypeMismatchArray : Opcode {

def IsConstantContext: Opcode;
def CheckAllocations : Opcode;
def CheckNull : Opcode;

def BitCastTypeClass : TypeClass {
let Types = [Uint8, Sint8, Uint16, Sint16, Uint32, Sint32, Uint64, Sint64,
Expand Down
5 changes: 2 additions & 3 deletions clang/test/AST/ByteCode/complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,9 @@ namespace ComplexConstexpr {
// both-note {{cannot refer to element 3 of array of 2 elements}}
constexpr _Complex float *p = 0;
constexpr float pr = __real *p; // both-error {{constant expr}} \
// expected-note {{read of dereferenced null pointer}} \
// ref-note {{dereferencing a null pointer}}
// both-note {{dereferencing a null pointer}}
constexpr float pi = __imag *p; // both-error {{constant expr}} \
// ref-note {{dereferencing a null pointer}}
// both-note {{dereferencing a null pointer}}
constexpr const _Complex double *q = &test3 + 1;
constexpr double qr = __real *q; // ref-error {{constant expr}} \
// ref-note {{cannot access real component of pointer past the end}}
Expand Down
3 changes: 3 additions & 0 deletions clang/test/AST/ByteCode/const-eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ typedef __INTPTR_TYPE__ intptr_t;
const intptr_t A = (intptr_t)(((int*) 0) + 1);
const intptr_t B = (intptr_t)(((char*)0) + 3);
_Static_assert(A > B, "");
int * GH149500_p = &(*(int *)0x400);
static const void *GH149500_q = &(*(const struct sysrq_key_op *)0);

#else
#error :(
#endif
5 changes: 2 additions & 3 deletions clang/test/AST/ByteCode/cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ struct S {
constexpr S s = { 5 };
constexpr const int *p = &s.m + 1;

constexpr const int *np2 = &(*(int(*)[4])nullptr)[0];
// ref-error@-1 {{constexpr variable 'np2' must be initialized by a constant expression}} \
// ref-note@-1 {{dereferencing a null pointer is not allowed in a constant expression}}
constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // both-error {{constexpr variable 'np2' must be initialized by a constant expression}} \
// both-note {{dereferencing a null pointer is not allowed in a constant expression}}

constexpr int preDec(int x) { // both-error {{never produces a constant expression}}
return --x; // both-note {{subexpression}}
Expand Down
6 changes: 2 additions & 4 deletions clang/test/AST/ByteCode/records.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1660,11 +1660,9 @@ namespace NullptrCast {
constexpr A *na = nullptr;
constexpr B *nb = nullptr;
constexpr A &ra = *nb; // both-error {{constant expression}} \
// ref-note {{dereferencing a null pointer}} \
// expected-note {{cannot access base class of null pointer}}
// both-note {{dereferencing a null pointer}}
constexpr B &rb = (B&)*na; // both-error {{constant expression}} \
// ref-note {{dereferencing a null pointer}} \
// expected-note {{cannot access derived class of null pointer}}
// both-note {{dereferencing a null pointer}}
constexpr bool test() {
auto a = (A*)(B*)nullptr;

Expand Down
8 changes: 8 additions & 0 deletions clang/test/CXX/drs/cwg14xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
// RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx11,since-cxx14,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx11,since-cxx14,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors

// RUN: %clang_cc1 -std=c++98 %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx11-17,since-cxx11, -fexceptions -fcxx-exceptions -pedantic-errors -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++14 %s -verify=expected,cxx14-17,cxx11-17,since-cxx11,since-cxx14 -fexceptions -fcxx-exceptions -pedantic-errors -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++17 %s -verify=expected,cxx14-17,cxx11-17,since-cxx11,since-cxx14 -fexceptions -fcxx-exceptions -pedantic-errors -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++20 %s -verify=expected,since-cxx11,since-cxx14,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx11,since-cxx14,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx11,since-cxx14,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors -fexperimental-new-constant-interpreter

namespace cwg1413 { // cwg1413: 12
template<int> struct Check {
typedef int type;
Expand Down
Loading