@@ -481,16 +481,21 @@ static void CheckForNullPointerDereference(Sema &S, Expr *E) {
481
481
// optimizer will delete, so warn about it. People sometimes try to use this
482
482
// to get a deterministic trap and are surprised by clang's behavior. This
483
483
// only handles the pattern "*null", which is a very syntactic check.
484
- if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts()))
485
- if (UO->getOpcode() == UO_Deref &&
486
- UO->getSubExpr()->IgnoreParenCasts()->
487
- isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) &&
484
+ const auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts());
485
+ if (UO && UO->getOpcode() == UO_Deref) {
486
+ const LangAS AS =
487
+ UO->getSubExpr()->getType()->getPointeeType().getAddressSpace();
488
+ if ((!isTargetAddressSpace(AS) ||
489
+ (isTargetAddressSpace(AS) && toTargetAddressSpace(AS) == 0)) &&
490
+ UO->getSubExpr()->IgnoreParenCasts()->isNullPointerConstant(
491
+ S.Context, Expr::NPC_ValueDependentIsNotNull) &&
488
492
!UO->getType().isVolatileQualified()) {
489
- S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO,
490
- S.PDiag(diag::warn_indirection_through_null)
491
- << UO->getSubExpr()->getSourceRange());
492
- S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO,
493
- S.PDiag(diag::note_indirection_through_null));
493
+ S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO,
494
+ S.PDiag(diag::warn_indirection_through_null)
495
+ << UO->getSubExpr()->getSourceRange());
496
+ S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO,
497
+ S.PDiag(diag::note_indirection_through_null));
498
+ }
494
499
}
495
500
}
496
501
0 commit comments