[Bug c++/107699] New: False positive -Warray-bounds, non-existent offset reported by GCC

carlosgalvezp at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Nov 15 14:10:37 GMT 2022


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.


More information about the Gcc-bugs mailing list