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

            Bug ID: 106238
           Summary: Inline optimization causes dangling pointer on
                    "include/c++/12.1.0/bits/stl_tree.h"
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rogerio.souza at gmail dot com
  Target Milestone: ---

Created attachment 53282
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53282&action=edit
Preprocessed file to reroduce the issue

When compiling the code below, we get a dangling pointer warning.

====================================================================
#include <map>

struct sysDLoc {                        /* Coordinates (in DBU) */
  double        x, y;
};

std::map<int*, sysDLoc> static_copy;

void realSwap()
{
  std::map<int*, sysDLoc> local_copy;
  extern void getLocalCopy(std::map<int*, sysDLoc>&);
  getLocalCopy(local_copy);
  local_copy.swap(static_copy);
}
====================================================================

Compilation command:
g++ -Wdangling-pointer -c -O2 bug.cpp


Warning log:
In file included from
/grid/common/test/gcc-v12.1.0d2rh74_lnx86/include/c++/12.1.0/map:60,
                 from bug.cpp:1:
In member function 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare,
_Alloc>::swap(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&) [with
_Key = int*; _Val = std::pair<int* const, sysDLoc>; _KeyOfValue =
std::_Select1st<std::pair<int* const, sysDLoc> >; _Compare = std::less<int*>;
_Alloc = std::allocator<std::pair<int* const, sysDLoc> >]',
    inlined from 'void std::map<_Key, _Tp, _Compare,
_Alloc>::swap(std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = int*; _Tp =
sysDLoc; _Compare = std::less<int*>; _Alloc = std::allocator<std::pair<int*
const, sysDLoc> >]' at
/grid/common/test/gcc-v12.1.0d2rh74_lnx86/include/c++/12.1.0/bits/stl_map.h:1172:18,
    inlined from 'void realSwap()' at bug.cpp:14:18:
/grid/common/test/gcc-v12.1.0d2rh74_lnx86/include/c++/12.1.0/bits/stl_tree.h:2090:32:
warning: storing the address of local variable 'local_copy' in '*MEM[(struct
_Rb_tree_node_base * &)&local_copy + 16].std::_Rb_tree_node_base::_M_paren
' [-Wdangling-pointer=]
 2090 |           _M_root()->_M_parent = _M_end();
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
bug.cpp: In function 'void realSwap()':
bug.cpp:11:27: note: 'local_copy' declared here
   11 |   std::map<int*, sysDLoc> local_copy;
      |                           ^~~~~~~~~~
bug.cpp:11:27: note: 'local_copy.std::map<int*, sysDLoc, std::less<int*>,
std::allocator<std::pair<int* const, sysDLoc> > >::_M_t.std::_Rb_tree<int*,
std::pair<int* const, sysDLoc>, std::_Select1st<std::pair<int* const, sysDLoc>
>, std::less<int*>, std::allocator<std::pair<int* const, sysDLoc> >
>::_M_impl.std::_Rb_tree<int*, std::pair<int* const, sysDLoc>,
std::_Select1st<std::pair<int* const, sysDLoc> >, std::less<int*>,
std::allocator<std::pair<int* const, sysDLoc> >
>::_Rb_tree_impl<std::less<int*>,
true>::<unnamed>.std::_Rb_tree_header::_M_header.std::_Rb_tree_node_base::_M_parent'
declared here

This warning only happens on GCC v12.1. Using -O1,
"-fno-inline-small-functions”, or  “-fno-inline-functions” prevents the error
from happening.

Regards,
Rogerio

Reply via email to