public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107699] New: False positive -Warray-bounds, non-existent offset reported by GCC
@ 2022-11-15 14:10 carlosgalvezp at gmail dot com
  2022-11-16 13:34 ` [Bug tree-optimization/107699] " rguenth at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: carlosgalvezp at gmail dot com @ 2022-11-15 14:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107699

            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 <array>
#include <algorithm>

std::size_t getCount();

int foo()
{
    std::array<int, 3> 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 <source>:2:
In function 'void std::__final_insertion_sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = int*; _Compare =
__gnu_cxx::__ops::_Iter_less_iter]',
    inlined from 'void std::__final_insertion_sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = int*; _Compare =
__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 = int*; _Compare =
__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 = int*; _Compare =
__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 = int*]' at
/opt/compiler-explorer/gcc-trunk-20221115/include/c++/13.0.0/bits/stl_algo.h:4860:18,
    inlined from 'int foo()' at <source>: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<int, 3> [1]'
[-Warray-bounds]
 1859 |           std::__insertion_sort(__first, __first + int(_S_threshold),
__comp);
      |          
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>: In function 'int foo()':
<source>:8:24: note: at offset 64 into object 'data' of size 12
    8 |     std::array<int, 3> 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.

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-12-15 13:57 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-15 14:10 [Bug c++/107699] New: False positive -Warray-bounds, non-existent offset reported by GCC carlosgalvezp at gmail dot com
2022-11-16 13:34 ` [Bug tree-optimization/107699] " rguenth at gcc dot gnu.org
2022-11-16 13:34 ` [Bug tree-optimization/107699] 12/13 Regression] " rguenth at gcc dot gnu.org
2022-11-16 14:54 ` [Bug tree-optimization/107699] [12/13 " carlosgalvezp at gmail dot com
2022-11-23  3:00 ` hp at gcc dot gnu.org
2022-11-29 13:22 ` rguenth at gcc dot gnu.org
2022-11-29 13:41 ` carlosgalvezp at gmail dot com
2022-11-29 13:46 ` carlosgalvezp at gmail dot com
2022-11-29 17:54 ` rguenther at suse dot de
2022-11-29 17:58 ` carlosgalvezp at gmail dot com
2022-12-08 10:58 ` cvs-commit at gcc dot gnu.org
2022-12-08 10:59 ` rguenth at gcc dot gnu.org
2023-02-16 10:37 ` carlosgalvezp at gmail dot com
2023-05-08 12:26 ` [Bug tree-optimization/107699] [12/13/14 " rguenth at gcc dot gnu.org
2023-12-15 13:57 ` rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).