[PATCH] D98798: Produce warning for performing pointer arithmetic on a null pointer.

Jamie Schmeiser via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 11 08:31:08 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rGdfc1e31d49fe: Produce warning for performing pointer arithmetic on a null pointer. (authored by jamieschmeiser).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98798/new/

https://reviews.llvm.org/D98798

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/pointer-addition.c
  clang/test/Sema/pointer-addition.cpp


Index: clang/test/Sema/pointer-addition.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/pointer-addition.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wextra -std=c++11
+
+void a() {
+  char *f = (char*)0;
+  f = (char*)((char*)0 - f); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
+  f = (char*)(f - (char*)0); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
+  f = (char*)((char*)0 - (char*)0); // valid in C++
+}
Index: clang/test/Sema/pointer-addition.c
===================================================================
--- clang/test/Sema/pointer-addition.c
+++ clang/test/Sema/pointer-addition.c
@@ -29,4 +29,7 @@
   // Cases that don't match the GNU inttoptr idiom get a different warning.
   f = (char*)0 - i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
   int *g = (int*)0 + i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
+  f = (char*)((char*)0 - f); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
+  f = (char*)(f - (char*)0); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
+  f = (char*)((char*)0 - (char*)0); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}} expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
 }
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -10779,7 +10779,17 @@
                                                LHS.get(), RHS.get()))
         return QualType();
 
-      // FIXME: Add warnings for nullptr - ptr.
+      bool LHSIsNullPtr = LHS.get()->IgnoreParenCasts()->isNullPointerConstant(
+          Context, Expr::NPC_ValueDependentIsNotNull);
+      bool RHSIsNullPtr = RHS.get()->IgnoreParenCasts()->isNullPointerConstant(
+          Context, Expr::NPC_ValueDependentIsNotNull);
+
+      // Subtracting nullptr or from nullptr should produce
+      // a warning expect nullptr - nullptr is valid in C++ [expr.add]p7
+      if (LHSIsNullPtr && (!getLangOpts().CPlusPlus || !RHSIsNullPtr))
+        diagnoseArithmeticOnNullPointer(*this, Loc, LHS.get(), false);
+      if (RHSIsNullPtr && (!getLangOpts().CPlusPlus || !LHSIsNullPtr))
+        diagnoseArithmeticOnNullPointer(*this, Loc, RHS.get(), false);
 
       // The pointee type may have zero size.  As an extension, a structure or
       // union may have zero size or an array may have zero length.  In this


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98798.344413.patch
Type: text/x-patch
Size: 2797 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210511/e247ee40/attachment-0001.bin>


More information about the cfe-commits mailing list