From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 649713857027; Fri, 21 Jan 2022 15:40:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 649713857027 From: "kbrabml at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/104165] New: Incorrectly identifying array bounds with -O2 -Werror=array-bounds Date: Fri, 21 Jan 2022 15:40:24 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: kbrabml at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Jan 2022 15:40:24 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104165 Bug ID: 104165 Summary: Incorrectly identifying array bounds with -O2 -Werror=3Darray-bounds Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: kbrabml at gmail dot com Target Milestone: --- In file included from /home/install/include/c++/12.0.1/algorithm:61, from repro.cpp:1: In function =E2=80=98void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator =3D int*; _Com= pare =3D __gnu_cxx::__ops::_Iter_comp_iter >]=E2= =80=99, inlined from =E2=80=98void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator =3D int*; _Com= pare =3D __gnu_cxx::__ops::_Iter_comp_iter >]=E2= =80=99 at /home/install/include/c++/12.0.1/bits/stl_algo.h:1940:31, inlined from =E2=80=98void std::sort(_RAIter, _RAIter, _Compare) [with = _RAIter =3D int*; _Compare =3D bar(int, int)::]=E2=80=99 at /home/install/include/c++/12.0.1/bits/stl_algo.h:4853:18, inlined from =E2=80=98int bar(int, int)=E2=80=99 at repro.cpp:17:14, inlined from =E2=80=98int foo(int)=E2=80=99 at repro.cpp:25:13: /home/install/include/c++/12.0.1/bits/stl_algo.h:1849:32: error: array subscript 16 is outside array bounds of =E2=80=98unsigned char [16]=E2=80=99 [-Werror=3Darray-bounds] 1849 | std::__insertion_sort(__first, __first + int(_S_threshold= ), __comp); |=20=20=20=20=20=20=20=20=20=20 ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In function =E2=80=98int bar(int, int)=E2=80=99, inlined from =E2=80=98int foo(int)=E2=80=99 at repro.cpp:25:13: repro.cpp:4:7: note: at offset 64 into object =E2=80=98f.140=E2=80=99 of si= ze 16 4 | int f[l]; | ^ cc1plus: all warnings being treated as errors make: *** [Makefile:5: all] Error 1 Getting the above bogus warning when compiling following reproducer #include static int bar(int n, int l) { // make function non-static and warning goes away int f[l]; int x =3D 0; int r =3D n; for (; x < l;) { if (r) { x =3D l; } else { // Take out this else and the warning goes away r =3D 1; } } if (r =3D=3D 1) { // Take out this branch and the warning goes away std::sort(f, f + x, [](int a, int b) { return a > b; }); } return 1; } int foo(int n) { return bar(n, 4); } Compiled with HEAD of GCC (as of 20/01/2021) g++ -c -march=3Darmv8-a -Werror -Wall -O2 repro.cpp The compiler sees that the size of the array is 4 ints (16-bytes), but does= n't see that x <=3D 4, so that no array out of bounds should occur. Workaround = is to use malloc for the array.=