aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ExprConstant.cpp
diff options
context:
space:
mode:
authorCorentin Jabot <corentinjabot@gmail.com>2025-07-19 20:30:16 +0300
committerGitHub <noreply@github.com>2025-07-19 19:30:16 +0200
commitc43f828d59672b4844a7409e4660b9f8f509da35 (patch)
tree7c30b8a884c12611ed53c41c3dd66f1e2dc16811 /clang/lib/AST/ExprConstant.cpp
parent1fcf49a35c4ffce2c0a8baf8a045c031f783ccff (diff)
downloadllvm-c43f828d59672b4844a7409e4660b9f8f509da35.zip
llvm-c43f828d59672b4844a7409e4660b9f8f509da35.tar.gz
llvm-c43f828d59672b4844a7409e4660b9f8f509da35.tar.bz2
[Clang] Be less strict about diagnosing null pointer dereference. (#149648)
In #143667, we made constant evaluation fail on `*null_ptr`, as this is UB. However, `&(*(foo*)0)` seems to be a common pattern, which made #143667 too disruptive. So instead of failing the evaluation, we note the UB, which let clang recovers when checking for constant initialization. Fixes #149500
Diffstat (limited to 'clang/lib/AST/ExprConstant.cpp')
-rw-r--r--clang/lib/AST/ExprConstant.cpp10
1 files changed, 7 insertions, 3 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index cfc4729..e575405 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -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) {