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

Clark Gaebel cg.wowus.cg at gmail.com
Tue Jul 12 09:53:06 PDT 2011

I don't see what you expect Clang to do about this.

The warning is perfectly accurate. The array is declared as size 1,
and you're indexing past it. If you have a function like

struct S {
     int a[1];

int get_second(struct S* s) {
    return s->a[1];

That's very clearly an out of bounds error unless very specific
conditions are met (heap allocated with enough space after the
struct...) I feel like it would be impossible to prove this in every

So that just leaves us with turning the warning off. (Which you can do
yourself with -Wno-array-bounds)

I wouldn't want this off by default if that's what you're suggesting,
since it's a silver bullet for off-by-one bugs:

void iterate() {
    int a[4];
    for(size_t i = 0; i <= 4; i++)
        a[i] += 4;

I want a warning for that!

  -- Clark

On Tue, Jul 12, 2011 at 11:54 AM, Peter Geoghegan <peter at 2ndquadrant.com> wrote:
> Saying "just use C99" isn't a practical solution. This is a mature
> project with a large codebase, that targets many compilers, including,
> for a large number of reasons, Visual Studio - Microsoft have
> absolutely no interest in pursuing C99 support.
> Besides, the chances of Clang actually helpfully diagnosing a problem
> with the delta between how GCC does -Warray-bounds and how Clang does
> it are slim - how often are these problems statically detectable? This
> is C.
> Even if your position wasn't unreasonable, which it is, Clang still
> gives this warning when the -c89 flag is given.
> --
> Peter Geoghegan       http://www.2ndQuadrant.com/
> PostgreSQL Development, 24x7 Support, Training and Services
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

More information about the cfe-dev mailing list