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

            Bug ID: 104017
           Summary: unexpeted -Warray-bounds popping a fixed number of
                    std::deque elements
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

I got the following report in my private mail.  I file it here for reference
(and my analysis).  Although the warning isn't terribly informative I don't
consider it a false positive.

$ cat t.C && g++ -O2 -S -Wall t.C
#include <deque>

struct Node { Node const * parent = nullptr; };

void func(Node const * n)
{
    std::deque<Node const *> p;

    Node const * e = n;
    while (e != nullptr) {
        p.push_front(e);
        e = e->parent;
    }

    [[maybe_unused]] Node const * b;

    p.pop_front();
    e = p.front();
    p.pop_front();

    b = p.back();
    p.pop_back();
}
In file included from
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/allocator.h:46,
                 from
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/deque:61,
                 from t.C:1:
In member function ‘void std::__new_allocator<_Tp>::destroy(_Up*) [with _Up =
const Node*; _Tp = const Node*]’,
    inlined from ‘static void std::allocator_traits<std::allocator<_Tp1>
>::destroy(allocator_type&, _Up*) [with _Up = const Node*; _Tp = const Node*]’
at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/alloc_traits.h:535:15,
    inlined from ‘void std::deque<_Tp, _Alloc>::pop_back() [with _Tp = const
Node*; _Alloc = std::allocator<const Node*>]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_deque.h:1604:28,
    inlined from ‘void func(const Node*)’ at t.C:22:15:
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/new_allocator.h:181:11:
warning: array subscript -1 is outside array bounds of ‘const Node* [64]’
[-Warray-bounds]
  181 |         { __p->~_Up(); }
      |           ^~~
In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const
void*) [with _Tp = const Node*]’,
    inlined from ‘static _Tp* std::allocator_traits<std::allocator<_Tp1>
>::allocate(allocator_type&, size_type) [with _Tp = const Node*]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/alloc_traits.h:464:28,
    inlined from ‘std::_Deque_base<_Tp, _Alloc>::_Ptr std::_Deque_base<_Tp,
_Alloc>::_M_allocate_node() [with _Tp = const Node*; _Alloc =
std::allocator<const Node*>]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_deque.h:583:26,
    inlined from ‘void std::_Deque_base<_Tp,
_Alloc>::_M_create_nodes(_Map_pointer, _Map_pointer) [with _Tp = const Node*;
_Alloc = std::allocator<const Node*>]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_deque.h:684:37,
    inlined from ‘void std::_Deque_base<_Tp,
_Alloc>::_M_initialize_map(std::size_t) [with _Tp = const Node*; _Alloc =
std::allocator<const Node*>]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_deque.h:658:19,
    inlined from ‘std::_Deque_base<_Tp, _Alloc>::_Deque_base() [with _Tp =
const Node*; _Alloc = std::allocator<const Node*>]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_deque.h:460:26,
    inlined from ‘std::deque<_Tp, _Alloc>::deque() [with _Tp = const Node*;
_Alloc = std::allocator<const Node*>]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_deque.h:855:7,
    inlined from ‘void func(const Node*)’ at t.C:7:30:
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/new_allocator.h:137:55:
note: at offset -8 into object of size 512 allocated by ‘operator new’
  137 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n *
sizeof(_Tp)));
      |                                                       ^

Reply via email to