https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120281
Bug ID: 120281
Summary: -Warray-bounds produces an incorrect warning when
compiled with -O3 and --coverage
Product: gcc
Version: 14.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: dominick.allen1989 at gmail dot com
Target Milestone: ---
Created attachment 61425
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61425&action=edit
Original bug.cpp, preprocessed file, and cvise reduction of preprocessed fille.
I encountered this on gentoo, on AMD64, with gcc 14.2. I have included
additional details about my version of gcc at the bottom of this post.
I have attached report.tar.gz, which provides bug.cpp (what I consider a
minimal reproducing example), bug.ii.orig which is a preprocessed version of
bug.cpp, bug.ii which is the output of cvise having run the reduce.sh script
also present in the archive. The gcc_info.txt file provides the output of the
command `g++ -v -save-temps -std=c++20 -Werror -Warray-bounds -O3 --coverage
bug.cpp 2>&1 | tee gcc_info.txt`. I have also included the file outputs from
that command, a-bug.ii and a-bug.s.
When compiling my original minimal case (https://godbolt.org/z/4Mb795ch3), I
get the following error when using -std=c++20 -O3 -Werror -Warray-bounds
--coverage.
```
libfud@localhost ~/Documents/bug_reports/cstr_warray_bounds $ make
g++ bug.cpp -std=c++20 -Werror -Warray-bounds -O3 --coverage -o bug
In function ‘constexpr Result<long unsigned int, FudStatus>
cStringLengthImpl(const T*, size_t) [with T = char]’,
inlined from ‘constexpr Result<long unsigned int, FudStatus>
cStringLength(const char*, size_t)’ at bug.cpp:141:44,
inlined from ‘constexpr Result<long unsigned int, FudStatus>
cStringLength(const char*)’ at bug.cpp:146:47,
inlined from ‘int main()’ at bug.cpp:150:20:
bug.cpp:132:12: error: array subscript -2 is outside array bounds of ‘const
char [2]’ [-Werror=array-bounds=]
132 | if (str[size] != 0 && size == maxLength) {
| ~~~^
cc1plus: all warnings being treated as errors
make: *** [Makefile:5: bug] Error 1
```
Removing the --coverage flag, or uncommenting lines 124 and 130 in the attached
bug.cpp, makes the warning go away. This behavior is also replicable on godbolt
with (all x86-64) gcc 15.1, gcc 13.3, and gcc 12.4. It does not happen with gcc
11.4.
System information:
gcc (Gentoo 14.2.1_p20241221 p7) 14.2.1 20241221
sys-devel/gcc-14.2.1_p20241221:14::gentoo USE="cet (cxx)
(default-stack-clash-protection) (default-znow) fortran jit (multilib) nls
openmp (pie) sanitize ssp zstd -ada (-custom-cflags) -d -debug -doc
(-fixed-point) -go -graphite -hardened (-ieee-long-double) (-libssp) -lto
-modula2 -objc -objc++ -objc-gc (-pch) -pgo -rust -systemtap -test (-time64)
-valgrind -vanilla -vtv"