]> granicus.if.org Git - clang/commitdiff
PR10837: Warn if a null pointer constant is formed by a zero integer constant
authorRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 13 Nov 2013 01:24:28 +0000 (01:24 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 13 Nov 2013 01:24:28 +0000 (01:24 +0000)
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
test/Sema/warn-null.c [new file with mode: 0644]

index 3c6b3b046de904997f3d334d3b65183bf2952182..7cd567050934645a065a43a81d04ef2f8971106f 100644 (file)
@@ -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 (file)
index 0000000..8ac8c5c
--- /dev/null
@@ -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}}