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

Nick Desaulniers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 3 17:16:19 PDT 2021


nickdesaulniers added a comment.

I didn't see any instances in quick testing on the Linux kernel: x86_64 defconfig, aarch64 defconfig, arm defconfig, x86_64 allmodconfig.  So I guess that's a good thing (for Linux)!



================
Comment at: clang/lib/Sema/SemaExpr.cpp:10723-10733
+      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
----------------
Might it be better to move the C++ check to the top; have all of this within a `if (!getLangOpts().CPlusPlus) {` block? Perhaps I'm misunderstanding what the `||` is doing?


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

https://reviews.llvm.org/D98798



More information about the cfe-commits mailing list