https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94580

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Blocks|                            |56456

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
GCC does a little better with zero-length arrays:

$ cat t.c && gcc -O2 -S -Wall -Wextra -o/dev/stdout t.c
struct A { int n, a[0]; };
struct B { struct A a; int x; };

struct B b;

int f0 (void)
{
  b.a.a[1] = 1;
  int t = b.x;
  b.a.a[1] = 0;
  return b.x - t;
}

int f1 (struct B *p)
{
  p->a.a[1] = 1;
  int t = p->x;
  p->a.a[1] = 0;
  return p->x - t;
}

        .file   "t.c"
        .text
t.c: In function ‘f0’:
t.c:10:8: warning: array subscript 1 is above array bounds of ‘int[0]’
[-Warray-bounds]
   10 |   b.a.a[1] = 0;
      |   ~~~~~^~~
t.c:1:19: note: while referencing ‘a’
    1 | struct A { int n, a[0]; };
      |                   ^
        .p2align 4
        .globl  f0
        .type   f0, @function
f0:
.LFB0:
        .cfi_startproc
        movl    $0, b+8(%rip)
        xorl    %eax, %eax
        ret
        .cfi_endproc
.LFE0:
        .size   f0, .-f0
t.c: In function ‘f1’:
t.c:18:9: warning: array subscript 1 is outside the bounds of an interior
zero-length array ‘int[0]’ [-Wzero-length-bounds]
   18 |   p->a.a[1] = 0;
      |   ~~~~~~^~~
t.c:1:19: note: while referencing ‘a’
    1 | struct A { int n, a[0]; };
      |                   ^
        .p2align 4
        .globl  f1
        .type   f1, @function
f1:
.LFB1:
        .cfi_startproc
        movl    $0, 8(%rdi)
        xorl    %eax, %eax
        ret
        .cfi_endproc
.LFE1:
        .size   f1, .-f1
        .globl  b
        .bss
        .align 8
        .type   b, @object
        .size   b, 8
b:
        .zero   8
        .ident  "GCC: (GNU) 10.0.1 20200413 (experimental)"
        .section        .note.GNU-stack,"",@progbits


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56456
[Bug 56456] [meta-bug] bogus/missing -Warray-bounds

Reply via email to