[PATCH] Implement a warning for bogus sizeof(pointer) / sizeof(pointer[0])

Bernd Edlinger bernd.edlinger@hotmail.de
Thu Apr 20 21:33:00 GMT 2017


Hi!


This implements a new -Wall enabled warning for a rather common, but
completely wrong way to compute an array size by dividing the
sizeof(pointer) / sizeof(pointer[0]) or sizeof(*pointer).

It is often hard to find this kind of error by simple code inspection
in real code, because using sizeof in this way is a quite common idiom
to get the array size of an array variable.  And furthermore this
expression may be used in macros, which makes it even more important to
have this warning.

There is a similar warning -Wsizeof-pointer-memaccess which helped in
implementing the infrastructure for the new warning in the C FE.

However I noticed that the -Wsizeof-pointer-memaccess warning was 
missing in C, when the sizeof is used inside parentheses, which is
different from C++, so I fixed that too.

Of course, I added some test cases for that as well.

To illustrate the usefulness of this warning, it revealed quite a few
places where bogus sizeof divisions were used in our testsuite.


Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
Is it OK for trunk?


Thanks
Bernd.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: changelog-warn-sizeof-pointer.txt
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20170420/33728a56/attachment.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch-warn-sizeof-pointer.diff
Type: text/x-patch
Size: 24914 bytes
Desc: patch-warn-sizeof-pointer.diff
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20170420/33728a56/attachment.bin>


More information about the Gcc-patches mailing list