[PATCH] Fix to PR10837 - Warn when assigning null character to pointer

Ivan A. Kosarev ivan at ivan-labs.com
Tue Nov 12 02:29:43 PST 2013


  Fixed as suggested. Thanks for reviewing.

Hi rsmith,

http://llvm-reviews.chandlerc.com/D2124

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2124?vs=5427&id=5461#toc

Files:
  lib/Sema/SemaExpr.cpp
  test/Sema/PR10837.c

Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -10614,8 +10614,17 @@
 
   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;
Index: test/Sema/PR10837.c
===================================================================
--- test/Sema/PR10837.c
+++ test/Sema/PR10837.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+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}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2124.3.patch
Type: text/x-patch
Size: 1328 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131112/49a01c65/attachment.bin>


More information about the cfe-commits mailing list