This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/81141] missing warning using sizeof a/sizeof *a with a zero-length array
- From: "msebor at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 20 Jun 2017 17:58:45 +0000
- Subject: [Bug c/81141] missing warning using sizeof a/sizeof *a with a zero-length array
- Auto-submitted: auto-generated
- References: <bug-81141-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81141
--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
On second thought, the zero-length array misuses aren't limited to the (sizeof
P / sizeof *P) pattern. When the type of *P is one byte wide they extend even
to sizeof P. For example, the following call to strncpy is likely a mistake
and would benefit from being diagnosed as well. So perhaps -Wsizeof-array
might be a better name for the new option.
$ cat z.c && gcc -O2 -S -Wall z.c
struct S { char n, a[1]; };
void h (struct S *s)
{
__builtin_strncpy (s->a, "123", sizeof s->a);
}
There are actually two reasons for the code above to be considered buggy: one
is that the sizeof s->a expression is being applied to what is likely a
poor-man's fexible array, and another is that the call results in truncating
the copied string, leaving it unterminated. The truncation should be diagnosed
independently of the latter problem (i.e., even if the array is not a
zero-length array or an array of size 1 that's the last member of a struct).