https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89550
Bug ID: 89550 Summary: Spurious array-bounds warning when using __PRETTY_FUNCTION__ as a string_view. Product: gcc Version: 8.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: aaron at bestateless dot com Target Milestone: --- Created attachment 45870 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45870&action=edit Preprocessed source. Compiler Information: $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp --enable-cet=auto Thread model: posix gcc version 8.2.1 20181127 (GCC) Example: #include <string> void foo() { std::string_view s(__PRETTY_FUNCTION__); s.remove_prefix(s.find_last_of(' ')); std::string().find(s.data()); } Command Line: g++ -std=c++17 -Warray-bounds -Werror -O2 -c test.cpp -o test.o Does not fail for optimization levels -O0, -O1, or -O3. Only fails for -O2. Expected: Compiles successfully. From experiments on https://godbolt.org/ this code compiles on 7.1, 7.2, 7.3, 7.4, 8.1, 8.2, and trunk. It fails to compile on 8.2.1 and 8.3.0. Actual: test.cpp: In function ‘void foo()’: test.cpp:3:6: warning: offset ‘-1’ outside bounds of constant string [-Warray-bounds] void foo() { ^~~ Details: This does not fail if the line with "remove_prefix" is commented out. In this case __PRETTY_FUNCTION__ does contain a space, so it should not get npos from the "find_last_of" call. Also does not fail if the call to "find" is commented out. It is the combination of both lines that causes the warning. I can get this to fail in the same way on -O3 if I wrap the function in a namespace name that is six characters in length or more. If it is five characters or less then it does compile on -O3. This example succeeds with "g++ -std=c++17 -Warray-bounds -Werror -O3": #include <string> namespace nnnnn { void foo() { std::string_view s(__PRETTY_FUNCTION__); s.remove_prefix(s.find_last_of(' ')); std::string().find(s.data()); } } This example fails with "g++ -std=c++17 -Warray-bounds -Werror -O3" (note the namespace name is longer by one character): #include <string> namespace nnnnnn { void foo() { std::string_view s(__PRETTY_FUNCTION__); s.remove_prefix(s.find_last_of(' ')); std::string().find(s.data()); } }