Skip to content

[Clang] Be less strict about diagnosing null pointer dereference. #149648

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
10 changes: 7 additions & 3 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9346,9 +9346,13 @@ bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
// [C++26][expr.unary.op]
// If the operand points to an object or function, the result
// denotes that object or function; otherwise, the behavior is undefined.
return Success &&
(!E->getType().getNonReferenceType()->isObjectType() ||
findCompleteObject(Info, E, AK_Dereference, Result, E->getType()));
// Because &(*(type*)0) is a common pattern, we do not fail the evaluation
// immediately.
if (!Success || !E->getType().getNonReferenceType()->isObjectType())
return Success;
return bool(findCompleteObject(Info, E, AK_Dereference, Result,
E->getType())) ||
Info.noteUndefinedBehavior();
}

bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
Expand Down
2 changes: 0 additions & 2 deletions clang/test/AST/ByteCode/const-eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ struct s {
};

EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1));
// ref-error@-1 {{expression is not an integer constant expression}} \
// ref-note@-1 {{dereferencing a null pointer}}

#ifndef NEW_INTERP
EVAL_EXPR(20, __builtin_constant_p(*((int*) 10)));
Expand Down
7 changes: 4 additions & 3 deletions clang/test/Sema/const-eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ struct s {
};

EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1));
// expected-error@-1 {{not an integer constant expression}} \
// expected-note@-1 {{dereferencing a null pointer is not allowed in a constant expression}}


EVAL_EXPR(20, __builtin_constant_p(*((int*) 10)));

Expand Down Expand Up @@ -153,3 +150,7 @@ struct PR35214_X {
int PR35214_x;
int PR35214_y = ((struct PR35214_X *)&PR35214_x)->arr[1]; // expected-error {{not a compile-time constant}}
int *PR35214_z = &((struct PR35214_X *)&PR35214_x)->arr[1]; // ok, &PR35214_x + 2


int * GH149500_p = &(*(int *)0x400);
static const void *GH149500_q = &(*(const struct sysrq_key_op *)0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a test w/ NULL?

5 changes: 5 additions & 0 deletions clang/test/SemaCXX/constant-expression-cxx14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,3 +1445,8 @@ static_assert(test_member_null(), "");

}
}

namespace GH149500 {
unsigned int * p = &(*(unsigned int *)0x400);
static const void *q = &(*(const struct sysrq_key_op *)0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also nullptr test?

}