[PATCH] Fix to PR10837 - Warn when assigning null character to pointer
Ivan A. Kosarev
ivan at ivan-labs.com
Sat Nov 9 00:52:13 PST 2013
A test case added.
Hi rnk,
http://llvm-reviews.chandlerc.com/D2124
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D2124?vs=5412&id=5427#toc
Files:
lib/Sema/SemaExpr.cpp
test/Sema/PR10837.c
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -10614,6 +10614,15 @@
switch (ConvTy) {
case Compatible:
+ // 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;
Index: test/Sema/PR10837.c
===================================================================
--- /dev/null
+++ test/Sema/PR10837.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 %s -verify -pedantic -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.2.patch
Type: text/x-patch
Size: 1174 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131109/25cb28cd/attachment.bin>
More information about the cfe-commits
mailing list