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

            Bug ID: 123031
           Summary: False positive -Warray-bounds triggered by std::sort
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: [email protected]
  Target Milestone: ---

Created attachment 63004
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=63004&action=edit
Example that triggers the warning

Attached example triggers a false-positive -Warray-bounds warning with -O3
optimization for GCC 15.2.0 built with Homebrew for MacOS.

Invocation:
   g++-15 -Wall -O3 -c foo.cpp

Output:

In file included from
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algobase.h:71,
                 from /usr/local/Cellar/gcc/15.2.0/include/c++/15/algorithm:62,
                 from foo.cpp:1:
In member function 'constexpr bool
__gnu_cxx::__ops::_Iter_less_iter::operator()(_Iterator1, _Iterator2) const
[with _Iterator1 = unsigned int*; _Iterator2 = unsigned int*]',
    inlined from 'void std::__move_median_to_first(_Iterator, _Iterator,
_Iterator, _Iterator, _Compare) [with _Iterator = unsigned int*; _Compare =
__gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:91:17,
    inlined from '_RandomAccessIterator
std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator,
_Compare) [with _RandomAccessIterator = unsigned int*; _Compare =
__gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1856:34,
    inlined from 'void std::__introsort_loop(_RandomAccessIterator,
_RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = unsigned
int*; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1890:38,
    inlined from 'void std::__sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = unsigned int*;
_Compare = __gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1906:25,
    inlined from 'void std::sort(_RAIter, _RAIter) [with _RAIter = unsigned
int*]' at /usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:4841:18,
    inlined from 'auto Foo::operator()(std::size_t) const' at foo.cpp:18:15,
    inlined from 'int main()' at foo.cpp:34:21:
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/predefined_ops.h:45:23:
warning: array subscript 12 is outside array bounds of 'Foo [1]'
[-Warray-bounds=]
   45 |       { return *__it1 < *__it2; }
      |                ~~~~~~~^~~~~~~~
foo.cpp: In function 'int main()':
foo.cpp:31:8: note: at offset 48 into object 'foo' of size 48
   31 |    Foo foo;
      |        ^~~
In file included from
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_pair.h:61,
                 from
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algobase.h:64:
In function 'std::_Require<std::__not_<std::__is_tuple_like<_Tp> >,
std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&,
_Tp&) [with _Tp = unsigned int]',
    inlined from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2)
[with _ForwardIterator1 = unsigned int*; _ForwardIterator2 = unsigned int*]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algobase.h:186:11,
    inlined from 'void std::__move_median_to_first(_Iterator, _Iterator,
_Iterator, _Iterator, _Compare) [with _Iterator = unsigned int*; _Compare =
__gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:105:16,
    inlined from '_RandomAccessIterator
std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator,
_Compare) [with _RandomAccessIterator = unsigned int*; _Compare =
__gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1856:34,
    inlined from 'void std::__introsort_loop(_RandomAccessIterator,
_RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = unsigned
int*; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1890:38,
    inlined from 'void std::__sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = unsigned int*;
_Compare = __gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1906:25,
    inlined from 'void std::sort(_RAIter, _RAIter) [with _RAIter = unsigned
int*]' at /usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:4841:18,
    inlined from 'auto Foo::operator()(std::size_t) const' at foo.cpp:18:15,
    inlined from 'int main()' at foo.cpp:34:21:
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/move.h:237:11: warning: array
subscript 12 is outside array bounds of 'Foo [1]' [-Warray-bounds=]
  237 |       __b = _GLIBCXX_MOVE(__tmp);
      |           ^
foo.cpp: In function 'int main()':
foo.cpp:31:8: note: at offset 48 into object 'foo' of size 48
   31 |    Foo foo;
      |        ^~~
In function 'std::_Require<std::__not_<std::__is_tuple_like<_Tp> >,
std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&,
_Tp&) [with _Tp = unsigned int]',
    inlined from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2)
[with _ForwardIterator1 = unsigned int*; _ForwardIterator2 = unsigned int*]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algobase.h:186:11,
    inlined from 'void std::__move_median_to_first(_Iterator, _Iterator,
_Iterator, _Iterator, _Compare) [with _Iterator = unsigned int*; _Compare =
__gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:94:20,
    inlined from '_RandomAccessIterator
std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator,
_Compare) [with _RandomAccessIterator = unsigned int*; _Compare =
__gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1856:34,
    inlined from 'void std::__introsort_loop(_RandomAccessIterator,
_RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = unsigned
int*; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1890:38,
    inlined from 'void std::__sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = unsigned int*;
_Compare = __gnu_cxx::__ops::_Iter_less_iter]' at
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:1906:25,
    inlined from 'void std::sort(_RAIter, _RAIter) [with _RAIter = unsigned
int*]' at /usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/stl_algo.h:4841:18,
    inlined from 'auto Foo::operator()(std::size_t) const' at foo.cpp:18:15,
    inlined from 'int main()' at foo.cpp:34:21:
/usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/move.h:237:11: warning: array
subscript 12 is outside array bounds of 'Foo [1]' [-Warray-bounds=]
  237 |       __b = _GLIBCXX_MOVE(__tmp);
      |           ^
foo.cpp: In function 'int main()':
foo.cpp:31:8: note: at offset 48 into object 'foo' of size 48
   31 |    Foo foo;
      |        ^~~

Reply via email to