diff options
author | Corentin Jabot <corentinjabot@gmail.com> | 2025-07-19 20:30:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-19 19:30:16 +0200 |
commit | c43f828d59672b4844a7409e4660b9f8f509da35 (patch) | |
tree | 7c30b8a884c12611ed53c41c3dd66f1e2dc16811 /clang/lib/AST/ExprConstant.cpp | |
parent | 1fcf49a35c4ffce2c0a8baf8a045c031f783ccff (diff) | |
download | llvm-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.cpp | 10 |
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) { |