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

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 9 10:58:34 PDT 2021


efriedma added inline comments.


================
Comment at: clang/lib/Sema/SemaExpr.cpp:10724
+      // Subtracting from a null pointer should produce a warning.
+      if (LHS.get()->IgnoreParenCasts()->isNullPointerConstant(
+              Context, Expr::NPC_ValueDependentIsNotNull))
----------------
IgnoreParenCasts() seems a little weird here; e.g. this code thinks `(char*)(char)256` isn't null?  Not sure how much it practically matters, though, given this is just a warning.


================
Comment at: clang/test/Sema/pointer-addition.c:34
+  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}}
 }
----------------
This is what I was afraid would happen.

C++ has a specific carveout in the standard that "null-null" isn't undefined. C doesn't, but I'm not sure warning is practically useful there.

In any case, printing the same warning twice isn't useful.


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

https://reviews.llvm.org/D98798



More information about the cfe-commits mailing list