From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id BF4A6382E534; Tue, 15 Nov 2022 14:10:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BF4A6382E534 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668521437; bh=DTKX993EXKnuurAi6NQ6VmWSTA8apyPkNdpTOv7HW00=; h=From:To:Subject:Date:From; b=dKEI36o+u4o1N9L5JCgHd9mz5Jqkji6GZO2Idz54L3eT9xC3U2TeK7E5c5G6AuPpW BQ43zk5sTo86LbT0idTVMptqDpwmQYTHSOBbfqb08FRsQhyVMi59fKD7E5XuK8KEy0 CQMLaLif5BcAvKk85c7T1MiNut+caDKOWDyv9WJM= From: "carlosgalvezp at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/107699] New: False positive -Warray-bounds, non-existent offset reported by GCC Date: Tue, 15 Nov 2022 14:10:37 +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: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: carlosgalvezp 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107699 Bug ID: 107699 Summary: False positive -Warray-bounds, non-existent offset reported by GCC Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: carlosgalvezp at gmail dot com Target Milestone: --- Hi, The following code: #include #include std::size_t getCount(); int foo() { std::array data{3, 2, 1}; std::sort(data.begin(), data.begin() + getCount()); return data.front(); } Built with -Wall -O3 triggers: In file included from /opt/compiler-explorer/gcc-trunk-20221115/include/c++/13.0.0/algorithm:61, from :2: In function 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator =3D int*; _Com= pare =3D __gnu_cxx::__ops::_Iter_less_iter]', inlined from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator =3D int*; _Com= pare =3D __gnu_cxx::__ops::_Iter_less_iter]' at /opt/compiler-explorer/gcc-trunk-20221115/include/c++/13.0.0/bits/stl_algo.= h:1854:5, inlined from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator =3D int*; _Com= pare =3D __gnu_cxx::__ops::_Iter_less_iter]' at /opt/compiler-explorer/gcc-trunk-20221115/include/c++/13.0.0/bits/stl_algo.= h:1950:31, inlined from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator =3D int*; _Com= pare =3D __gnu_cxx::__ops::_Iter_less_iter]' at /opt/compiler-explorer/gcc-trunk-20221115/include/c++/13.0.0/bits/stl_algo.= h:1942:5, inlined from 'void std::sort(_RAIter, _RAIter) [with _RAIter =3D int*]'= at /opt/compiler-explorer/gcc-trunk-20221115/include/c++/13.0.0/bits/stl_algo.= h:4860:18, inlined from 'int foo()' at :9:14: /opt/compiler-explorer/gcc-trunk-20221115/include/c++/13.0.0/bits/stl_algo.= h:1859:32: warning: array subscript 16 is outside array bounds of 'std::array = [1]' [-Warray-bounds] 1859 | std::__insertion_sort(__first, __first + int(_S_threshold= ), __comp); |=20=20=20=20=20=20=20=20=20=20 ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ : In function 'int foo()': :8:24: note: at offset 64 into object 'data' of size 12 8 | std::array data{3, 2, 1}; | ^~~~ Compiler returned: 0 Repro: https://godbolt.org/z/Ma8KK1MKE There is nowhere in the code any "offset 64". The compiler cannot possibly = know if there is OOB or not, since that depends on the function "getCount" which= is implemented in a separate translation unit. Therefore the warning violates what the documentation says: "It warns about subscripts to arrays that are always out of bounds" This is not true in this case. It's not "always" out of bounds.=