This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/81141] missing warning using sizeof a/sizeof *a with a zero-length array


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).

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]