[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
Fri Apr 9 14:08:13 PDT 2021


jamieschmeiser added inline comments.


================
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}}
 }
----------------
jamieschmeiser wrote:
> efriedma wrote:
> > 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.
> Yes, I see that [expr.add] p 7 says nullptr-nullptr is defined to be 0.  I will suppress the warning for this.
I disagree with the comment that the two warnings are not useful.  While it is the same warning, the section of code indicated by each warning is different and both need to be corrected to clear the code of warnings.  A single warning would likely be more confusing in that a user would see the same warning and not notice that a different section of code is indicated after fixing the indicated problem.  I would expect the typical response to be the user assuming that they had made a mistake and removing the initial fix and fixing the second, only to again receive a same warning again with a different section of code.  The different code sections indicated would likely be overlooked, leading to frustration whereas two warnings clearly indicates there are 2 problems.


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

https://reviews.llvm.org/D98798



More information about the cfe-commits mailing list