https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105400
Bug ID: 105400 Summary: g++-11 regression produces -Warray-bounds false positive warning with -O2 Product: gcc Version: 11.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: laurent.pinchart at ideasonboard dot com Target Milestone: --- Created attachment 52887 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52887&action=edit Test case Starting with g++ 11, we noticed a -Warray-bounds warning when compiling the libcamera project. I've narrowed it down to the attached standalone test case (without any #include). The warning is produced with the following g++ options: $ g++-11.2.1 -v -save-temps -W -Wall -std=c++17 -O2 -c -o cast2.o cast2.cpp Using built-in specs. COLLECT_GCC=g++-11.2.1 Target: x86_64-pc-linux-gnu Configured with: /var/tmp/portage/sys-devel/gcc-11.2.1_p20220115/work/gcc-11-20220115/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/11.2.1 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11 --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --disable-libunwind-exceptions --enable-checking=yes --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 11.2.1_p20220115 p4' --disable-esp --enable-libstdcxx-time --disable-libstdcxx-pch --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libssp --disable-libada --disable-cet --disable-systemtap --disable-valgrind-annotations --disable-vtable-verify --disable-libvtv --without-zstd --enable-lto --with-isl --disable-isl-version-check --enable-default-pie --enable-default-ssp Thread model: posix Supported LTO compression algorithms: zlib gcc version 11.2.1 20220115 (Gentoo 11.2.1_p20220115 p4) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wextra' '-Wall' '-std=c++17' '-O2' '-c' '-o' 'cast2.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/cc1plus -E -quiet -v -D_GNU_SOURCE cast2.cpp -mtune=generic -march=x86-64 -std=c++17 -Wextra -Wall -O2 -fpch-preprocess -o cast2.ii ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11/x86_64-pc-linux-gnu /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11/backward /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include /usr/local/include /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include-fixed /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wextra' '-Wall' '-std=c++17' '-O2' '-c' '-o' 'cast2.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/cc1plus -fpreprocessed cast2.ii -quiet -dumpbase cast2.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64 -O2 -Wextra -Wall -std=c++17 -version -o cast2.s GNU C++17 (Gentoo 11.2.1_p20220115 p4) version 11.2.1 20220115 (x86_64-pc-linux-gnu) compiled by GNU C version 11.2.1 20220115, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096 GNU C++17 (Gentoo 11.2.1_p20220115 p4) version 11.2.1 20220115 (x86_64-pc-linux-gnu) compiled by GNU C version 11.2.1 20220115, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096 Compiler executable checksum: 8c115abe653dbf0f6d87498ebb5d37f4 cast2.cpp: In function ‘void func()’: cast2.cpp:25:26: warning: array subscript ‘Derived[0]’ is partly outside array bounds of ‘Base [1]’ [-Warray-bounds] 25 | d->count = 0; | ~~~~~~~~~^~~ cast2.cpp:36:14: note: while referencing ‘b’ 36 | Base b(Base::Type2); | ^ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wextra' '-Wall' '-std=c++17' '-O2' '-c' '-o' 'cast2.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/as -v --64 -o cast2.o cast2.s GNU assembler version 2.37 (x86_64-pc-linux-gnu) using BFD version (Gentoo 2.37_p1 p2) 2.37 COMPILER_PATH=/usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/:/usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/:/usr/libexec/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/ LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wextra' '-Wall' '-std=c++17' '-O2' '-c' '-o' 'cast2.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' 'cast2.' The out of bounds access is prevented by the type() check. The compiler can't predict the outcome of the check as it doesn't know that type() will return the type passed to the Base constructor.