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

            Bug ID: 114360
           Summary: Bogus -Wmaybe-uninitialized inside std::map internals
                    with -O3
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at jade dot fyi
  Target Milestone: ---

We found a GCC bug that was generating bogus warnings pointing into the
libstdc++ internals in the Nix project.

Create the file, say, meow.cc:

```
#include <map>
#include <string>
struct Base { bool b; std::map<int, int> m; };
struct Derived : Base { std::string s; };
void foo(const std::string & s) { Derived d{{}, s}; }
```

Compile with GCC 12.3.0 or 13.2.1, at least, with the following flags:

g++ -std=c++2a -O3 -c -Wall meow.cc

Observe bogus warning:

```
In file included from /usr/include/c++/13.2.1/map:62,
                 from meow.cc:1:
In member function ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val,
_KeyOfValue, _Compare, _Alloc>::_M_mbegin() const [with _Key = int; _Val =
std::pair<const int, int>; _KeyOfValue = std::_Select1st<std::pair<const int,
int> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int,
int> >]’,
    inlined from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val,
_KeyOfValue, _Compare, _Alloc>::_M_begin() [with _Key = int; _Val =
std::pair<const int, int>; _KeyOfValue = std::_Select1st<std::pair<const int,
int> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int,
int> >]’ at /usr/include/c++/13.2.1/bits/stl_tree.h:741:25,
    inlined from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare,
_Alloc>::~_Rb_tree() [with _Key = int; _Val = std::pair<const int, int>;
_KeyOfValue = std::_Select1st<std::pair<const int, int> >; _Compare =
std::less<int>; _Alloc = std::allocator<std::pair<const int, int> >]’ at
/usr/include/c++/13.2.1/bits/stl_tree.h:986:17,
    inlined from ‘std::map<_Key, _Tp, _Compare, _Alloc>::~map() [with _Key =
int; _Tp = int; _Compare = std::less<int>; _Alloc =
std::allocator<std::pair<const int, int> >]’ at
/usr/include/c++/13.2.1/bits/stl_map.h:314:7,
    inlined from ‘void foo(const std::string&)’ at meow.cc:5:50:
/usr/include/c++/13.2.1/bits/stl_tree.h:737:73: warning:
‘d.Derived::<unnamed>.Base::m.std::map<int, int, std::less<int>,
std::allocator<std::pair<const int, int> > >::_M_t.std::_Rb_tree<int,
std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >,
std::less<int>, std::allocator<std::pair<const int, int> >
>::_M_impl.std::_Rb_tree<int, std::pair<const int, int>,
std::_Select1st<std::pair<const int, int> >, std::less<int>,
std::allocator<std::pair<const int, int> > >::_Rb_tree_impl<std::less<int>,
true>::<anonymous>.std::_Rb_tree_header::_M_header.std::_Rb_tree_node_base::_M_parent’
may be used uninitialized [-Wmaybe-uninitialized]
  737 |       { return
static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
      |                                                                        
^
meow.cc: In function ‘void foo(const std::string&)’:
meow.cc:5:43: note: ‘d’ declared here
    5 | void foo(const std::string & s) { Derived d{{}, s}; }
      |                                           ^
```

Also reproduces in trunk 20240315 from godbolt here:
https://godbolt.org/z/d7z5ErM71

Reply via email to