This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/71780] std::map::operator[] crashes with std::vector of objects used as key


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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID
           Severity|major                       |normal

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
bool Small::operator<(const Small &rhs) const
{
  if (_field1 < rhs._field1) return(true);
  if (_field2 < rhs._field2) return(true);
  if (_field3 < rhs._field3) return(true);
  return(false);
}

This is not a valid StrictWeakOrdering, so it's undefined behaviour to put
those objects into a std::map. This is a bug in your code, not GCC.

Consider:

#include <cassert>

class Small {
public:
  Small() {}
  ~Small() {}

  bool operator<(const Small &rhs) const;
  int _field1;
  int _field2;
  int _field3;
};

bool Small::operator<(const Small &rhs) const
{
  if (_field1 < rhs._field1) return(true);
  if (_field2 < rhs._field2) return(true);
  if (_field3 < rhs._field3) return(true);
  return(false);
}

int
main()
{
  Small a, b;
  a._field1 = 1;
  a._field2 = 2;
  a._field3 = 0;
  b._field1 = 2;
  b._field2 = 1;
  b._field3 = 0;

  assert( ! ((a < b) && (b < a)) );
}

The assertion fails.

https://www.sgi.com/tech/stl/StrictWeakOrdering.html

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]