[cfe-dev] -Warray-bounds seems over-zealous on Clang

Reid Kleckner reid.kleckner at gmail.com
Tue Jul 12 15:52:48 PDT 2011

On Tue, Jul 12, 2011 at 6:39 PM, Peter Geoghegan <peter at 2ndquadrant.com> wrote:
> On 12 July 2011 18:29, Chris Lattner <clattner at apple.com> wrote:
>> Do we have empirical evidence that it finds bugs in arrays with exactly 1 element?  I think we should just disable it in the case that the array has a single element.  This really is a common pattern.
> I would think it reasonable if the warning was disabled for this exact
> pattern only.
> Disabling -Wno-array-bounds clearly isn't a practical measure, as most
> of the ways that that can trigger a warning are perfectly valid. I
> wouldn't be opposed to showing the warning where the struct is known
> to have been declared on the stack, but going that far probably isn't
> worth the effort.
> On 12 July 2011 17:45, Joerg Sonnenberger <joerg at britannica.bec.de> wrote:
>> Just because you don't agree doesn't make it unreasonable. The behavior
>> of the code doesn't change with the standard level -- you are still
>> declaring an array and overflowing it.
> I agree, it doesn't. The fact that this causes warnings for a very
> common pattern makes it unreasonable.

My inclination and I believe the inclination of others is that, if it
catches real bugs, which Nico has shown it has, then it's worth having
the warning.  Another compromise would be to have a flag about bounds
checks warnings for size 1 arrays at the end of structs (phew).  =P

If you can't use C99, are flexible arrays available as an extension in
clang, either in gnu89 or via some flag?  If so, you could use ifdefs
like this:

#ifdef __GNUC__
/* or some other condition, __has_feature or what have you */

struct Buffer {
  int len;
  char bytes[FLEXIBLE_ARRAY];

It's even self-documenting.


More information about the cfe-dev mailing list