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

--- Comment #1 from Chris Uzdavinis <cuzdav at gmail dot com> ---
Starting with Gcc12.1 (at least on x86) and through all versions up to the
trunk (post 13.1)
This warning hits with optimization -O2 or higher.

https://godbolt.org/z/q3T39Wf8c


    #include <vector>

    void foo(std::vector<std::pair<int,int>>& vec) {
        if (vec.size() > 5) {
            vec[3].first = 0;
        }
    }

    int main() {
        auto v1 = std::vector{std::pair(1, 2)};
        foo(v1);
    }

What is interesting to me is that the analyzer is missing the fact that size is
explicitly tested, and the index is a constant expression that is lower.

There are a lot of array-bounds bugs reported, and all have similarities and
I'm not sure if this is a dupe or not, though I went through the open ones in 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56456
And did not see an example quite like this.


In function 'void foo(std::vector<std::pair<int, int> >&)',
    inlined from 'int main()' at <source>:11:8:
<source>:5:22: error: array subscript 3 is outside array bounds of
'std::pair<int, int> [1]' [-Werror=array-bounds=]
    5 |         vec[3].first = 0;
In file included from
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/x86_64-linux-gnu/bits/c++allocator.h:33,
                 from
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/allocator.h:46,
                 from
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/vector:63,
                 from <source>:1:
In member function '_Tp* std::__new_allocator<_Tp>::allocate(size_type, const
void*) [with _Tp = std::pair<int, int>]',
    inlined from 'constexpr _Tp* std::allocator< <template-parameter-1-1>
>::allocate(std::size_t) [with _Tp = std::pair<int, int>]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/allocator.h:198:40,
    inlined from 'static constexpr _Tp*
std::allocator_traits<std::allocator<_Up> >::allocate(allocator_type&,
size_type) [with _Tp = std::pair<int, int>]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/alloc_traits.h:482:28,
    inlined from 'constexpr std::_Vector_base<_Tp, _Alloc>::pointer
std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp =
std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/stl_vector.h:378:33,
    inlined from 'constexpr void std::vector<_Tp,
_Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator,
std::forward_iterator_tag) [with _ForwardIterator = const std::pair<int, int>*;
_Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/stl_vector.h:1687:25,
    inlined from 'constexpr std::vector<_Tp,
_Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp =
std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/stl_vector.h:677:21,
    inlined from 'int main()' at <source>:10:42:
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/new_allocator.h:147:55:
note: at offset 24 into object of size 8 allocated by 'operator new'
  147 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n *
sizeof(_Tp)));
      |                                                       ^
cc1plus: all warnings being treated as errors
Compiler returned: 1

Reply via email to