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

            Bug ID: 111415
           Summary: False positive array-bounds warning with -O3
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: daiw at gmx dot net
  Target Milestone: ---

Using g++ (GCC) 13.2.0,

the following minimal example:



std::vector<int> foo(const std::vector<int>& a)
{
    if (a.size() < 2)
    {
        return a;
    }
    return a;
}

int main()
{
    foo(std::vector<int>({1}));
}



Produces a warning when trying to compile like so:


g++ -Wall -O3 main.cpp



Output:


In file included from /usr/local/include/c++/13.2.0/vector:62,
                 from main.cpp:1:
In static member function 'static _Up* std::__copy_move<_IsMove, true,
std::random_access_iterator_tag>::__copy_m(_Tp*, _Tp*, _Up*) [with _Tp = const
int; _Up = int; bool _IsMove = false]',
    inlined from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove =
false; _II = const int*; _OI = int*]' at
/usr/local/include/c++/13.2.0/bits/stl_algobase.h:506:30,
    inlined from '_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove =
false; _II = const int*; _OI = int*]' at
/usr/local/include/c++/13.2.0/bits/stl_algobase.h:533:42,
    inlined from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove =
false; _II = __gnu_cxx::__normal_iterator<const int*, vector<int> >; _OI =
int*]' at /usr/local/include/c++/13.2.0/bits/stl_algobase.h:540:31,
    inlined from '_OI std::copy(_II, _II, _OI) [with _II =
__gnu_cxx::__normal_iterator<const int*, vector<int> >; _OI = int*]' at
/usr/local/include/c++/13.2.0/bits/stl_algobase.h:633:7,
    inlined from 'static _ForwardIterator
std::__uninitialized_copy<true>::__uninit_copy(_InputIterator, _InputIterator,
_ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const
int*, std::vector<int> >; _ForwardIterator = int*]' at
/usr/local/include/c++/13.2.0/bits/stl_uninitialized.h:147:27,
    inlined from '_ForwardIterator std::uninitialized_copy(_InputIterator,
_InputIterator, _ForwardIterator) [with _InputIterator =
__gnu_cxx::__normal_iterator<const int*, vector<int> >; _ForwardIterator =
int*]' at /usr/local/include/c++/13.2.0/bits/stl_uninitialized.h:185:15,
    inlined from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator,
_InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator =
__gnu_cxx::__normal_iterator<const int*, vector<int> >; _ForwardIterator =
int*; _Tp = int]' at
/usr/local/include/c++/13.2.0/bits/stl_uninitialized.h:373:37,
    inlined from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp,
_Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]' at
/usr/local/include/c++/13.2.0/bits/stl_vector.h:603:31,
    inlined from 'std::vector<int> foo(const std::vector<int>&)' at
main.cpp:7:16:
/usr/local/include/c++/13.2.0/bits/stl_algobase.h:437:30: warning: 'void*
__builtin_memmove(void*, const void*, long unsigned int)' forming offset 4 is
out of the bounds [0, 4] [-Warray-bounds=]
  437 |             __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
      |             ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to