https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107787
Bug ID: 107787
Summary: -Werror=array-bounds=X does not work as expected
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: other
Assignee: unassigned at gcc dot gnu.org
Reporter: newtonist at yandex dot ru
Target Milestone: ---
Created attachment 53934
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53934&action=edit
Suggested fix patch
According to documentation the -Werror= option makes the specified warning into
an error and also automatically implies this option. Then it seems that the
behavior of the compiler when specifying -Werror=array-bounds=X should be the
same as specifying "-Werror=array-bounds -Warray-bounds=X", so we expect to
receive array-bounds pass triggers and they must be processed as errors. In
practice, we observe that the array-bounds pass is indeed called, but its
responses are processed as warnings, not errors.
$ cat testcase.c && gcc -O2 -Werror=array-bounds=1 -c testcase.c
int a[10];
int* f(void) {
a[-1] = 0;
return a;
}
testcase.c: In function ‘f’:
testcase.c:5:6: warning: array subscript -1 is below array bounds of ‘int[10]’
[-Warray-bounds]
5 | a[-1] = 0;
| ~^~~~
testcase.c:1:5: note: while referencing ‘a’
1 | int a[10];
| ^
As I understand, this happens because Warray-bounds and Warray-bounds= are
declared as 2 different options in common.opt, so when
diagnostic_classify_diagnostic() (opts-common.cc:1880) is called, DK_ERROR is
set for the Warray-bounds= option, but in diagnostic_report_diagnostic()
(diagnostic.cc:1446) through warning_at() passes opt_index of Warray-bounds, so
information about DK_ERROR is lost. My suggestion is to use Alias in
declaration of Warray-bounds (similarly as in Wattribute-alias etc.)
I plan to send this patch to the mailing list.