r277728 changed the formatting of zero-length arrays in diagnostics to include their bound, but the change also inadvertently added the zero bound to flexible array members (because they are gratuitously represented differently between the front-ends). The test case below shows the problem. It only affects C code: $ cat x.c && gcc -O2 -S -Wall x.c struct S0 { int n, a[0]; } s0; struct Sx { int n, a[]; } sx = { 0 }; void f (void) { s0.a[0] = 0; sx.a[0] = 0; } x.c: In function ‘f’: x.c:6:7: warning: array subscript 0 is above array bounds of ‘int[0]’ [-Warray-bounds] 6 | s0.a[0] = 0; | ~~~~^~~ x.c:1:20: note: while referencing ‘a’ 1 | struct S0 { int n, a[0]; } s0; | ^ x.c:1:28: note: defined here ‘s0’ 1 | struct S0 { int n, a[0]; } s0; | ^~ x.c:7:7: warning: array subscript 0 is above array bounds of ‘int[0]’ [-Warray-bounds] 7 | sx.a[0] = 0; | ~~~~^~~ x.c:2:20: note: while referencing ‘a’ 2 | struct Sx { int n, a[]; } sx = { 0 }; | ^ x.c:2:27: note: defined here ‘sx’ 2 | struct Sx { int n, a[]; } sx = { 0 }; | ^~ When compiled by the C++ front-end, the format of the arrays is as expected: $ gcc -O2 -S -Wall -xc++ x.c x.c: In function ‘void f()’: x.c:6:9: warning: array subscript 0 is above array bounds of ‘int [0]’ [-Warray-bounds] 6 | s0.a[0] = 0; | ~~~~~~^ x.c:1:20: note: while referencing ‘S0::a’ 1 | struct S0 { int n, a[0]; } s0; | ^ x.c:1:28: note: defined here ‘s0’ 1 | struct S0 { int n, a[0]; } s0; | ^~ x.c:7:9: warning: array subscript 0 is above array bounds of ‘int []’ [-Warray-bounds] 7 | sx.a[0] = 0; | ~~~~~~^ x.c:2:20: note: while referencing ‘Sx::a’ 2 | struct Sx { int n, a[]; } sx = { 0 }; | ^ x.c:2:27: note: defined here ‘sx’ 2 | struct Sx { int n, a[]; } sx = { 0 }; | ^~
Just for the record, I see the failure in grub2 package.
Any update on this Martin?
And for the record, qemu package build is also affected with this: /home/abuild/rpmbuild/BUILD/qemu-4.2.0/scsi/qemu-pr-helper.c: In function ‘multipath_pr_out’: /home/abuild/rpmbuild/BUILD/qemu-4.2.0/scsi/qemu-pr-helper.c:523:32: error: array subscript 0 is above array bounds of ‘struct transportid *[0]’ [-Werror=array-bounds] In file included from /home/abuild/rpmbuild/BUILD/qemu-4.2.0/scsi/qemu-pr-helper.c:36:
No progress yet. This bug is only cosmetic, in the format of the flexible array member in the warning, so it shouldn't cause any failures (as in false positives or negatives). Can you clarify how the builds of grub2 and qemu are affected by it?
> negatives). Can you clarify how the builds of grub2 and qemu are affected > by it? Despite our rules, these packages use -Werror ;) So it's not a critical issue this PR.
Patch: https://gcc.gnu.org/pipermail/gcc-patches/2020-April/543778.html
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>: https://gcc.gnu.org/g:09f041390245da60411a9f0e08c4bedf7430585a commit r10-7706-g09f041390245da60411a9f0e08c4bedf7430585a Author: Martin Sebor <msebor@redhat.com> Date: Mon Apr 13 15:31:44 2020 -0600 PR c/92326 - wrong bound in zero-length array diagnostics gcc/c-family/ChangeLog: PR c/92326 * c-pretty-print.c (c_pretty_printer::direct_abstract_declarator): Avoid printing array bound for flexible array members. gcc/testsuite/ChangeLog: PR c/92326 * c-c++-common/Warray-bounds-8.c: New test. * gcc.dg/Warray-bounds-46.c: Adjust expected format of flexible array memebrs in diagnostics. * gcc.dg/Warray-bounds-49.c: Same.
Fixed.
The qemu test-case is still reported as warning. Reduced test-case: $ cat qemu.i struct A a; int c, d; struct A { int scalar; int flexible[]; }; void g() { a.flexible[c] = d; } $ gcc -O2 -c qemu.i -Werror=array-bounds qemu.i: In function ‘g’: qemu.i:11:13: error: array subscript <unknown> is outside array bounds of ‘int[]’ [-Werror=array-bounds] 11 | a.flexible[c] = d; | ~~~~~~~~~~^~~ qemu.i:7:7: note: while referencing ‘flexible’ 7 | int flexible[]; | ^~~~~~~~ qemu.i:1:10: note: defined here ‘a’ 1 | struct A a; | ^ cc1: some warnings being treated as errors
This bug isn't about whether or not a warning is issued but about the format of the flexible array member printed by it: it should be 'int[]' but was 'int[0]'. (That's why Jeff and I have been wondering what you meant.) The warning in the test case in comment #9 is justified: the flexible array has no elements because struct a is defined but doesn't initialize it with any (there would be no warning if the struct were only declared but not defined). Hopefully this test case will explain it: $ cat pr92326.c && gcc -O2 -S -Wall pr92326.c struct A a; // a.flexible has no elements int c, d; struct A { int scalar; int flexible[]; }; void g() { a.flexible[c] = d; // out-of-bounds regardless of c } struct A a2 = { 1, { 0 } }; // b.flexible has 1 element void g2 (void) { a2.flexible[c] = d; // out-of-bounds only if c != 0 (probably should warn) } extern struct A a3; // b.flexible might have one or more elements void g3 (void) { a3.flexible[c] = d; // unknown if it's in-bounds or out-of-bounds } pr92326.c: In function ‘g’: pr92326.c:11:13: warning: array subscript <unknown> is outside array bounds of ‘int[]’ [-Warray-bounds] 11 | a.flexible[c] = d; // out-of-bounds regardless of c | ~~~~~~~~~~^~~ pr92326.c:7:7: note: while referencing ‘flexible’ 7 | int flexible[]; | ^~~~~~~~ pr92326.c:1:10: note: defined here ‘a’ 1 | struct A a; // a.flexible has no elements | ^
All right, thanks for the clarification. Btw. qemu fixed the warning in the meantime: https://github.com/patchew-project/qemu/commit/4ce1e15fbc7266a108a7c77a3962644b3935346e
Thanks! I'm glad to see the new warning has helpe identify (and fix) a real bug!