From f2febe6b5e9213fb4d97e1d4f9b47166be89afad Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 13 Nov 2013 01:24:28 +0000 Subject: [PATCH] PR10837: Warn if a null pointer constant is formed by a zero integer constant expression that is not a zero literal, in C. Patch by Ivan A. Kosarev! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194540 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExpr.cpp | 13 +++++++++++-- test/Sema/warn-null.c | 6 ++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 test/Sema/warn-null.c diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 3c6b3b046d..7cd5670509 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -10614,8 +10614,17 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, switch (ConvTy) { case Compatible: - DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr); - return false; + // See if a proper null pointer constant is to be assigned. + if (DstType->isAnyPointerType() && !SrcType->isAnyPointerType() && + SrcExpr->isNullPointerConstant(Context, + Expr::NPC_NeverValueDependent) == + Expr::NPCK_ZeroExpression && + !isUnevaluatedContext()) + Diag(SrcExpr->getExprLoc(), diag::warn_non_literal_null_pointer) + << DstType << SrcExpr->getSourceRange(); + + DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr); + return false; case PointerToInt: DiagKind = diag::ext_typecheck_convert_pointer_int; diff --git a/test/Sema/warn-null.c b/test/Sema/warn-null.c new file mode 100644 index 0000000000..8ac8c5c9d3 --- /dev/null +++ b/test/Sema/warn-null.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 %s -verify -fsyntax-only + +// PR10837: warn if a non-pointer-typed expression is folded to a null pointer +int *p = 0; +int *q = '\0'; // expected-warning{{expression which evaluates to zero treated as a null pointer constant}} +int *r = (1 - 1); // expected-warning{{expression which evaluates to zero treated as a null pointer constant}} -- 2.40.0