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

            Bug ID: 89608
           Summary: Undetected iterator invalidations on unordered
                    containers in debug mode
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mafrasi2 at gmail dot com
  Target Milestone: ---

Created attachment 45906
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45906&action=edit
Partial fix for unordered_set

Many iterator invalidations on unordered containers are not detected in debug
mode:

```
#include <debug/unordered_set>
#include <iostream>

int main() {
  __gnu_debug::unordered_set<int> myset;
  myset.insert(0);
  myset.insert(1);
  for (auto it = myset.begin(), end = myset.end(); it != end; ++it) {
    std::cout << *it << "\n";
    myset.insert(2);
  }
}
```

This example outputs `1` on my system and doesn't report the access to an
invalidated iterator.

The attached patch fixes the example and the same thing can be done to the
other affected containers. The bug can still be triggered with an explicit
rehash, though.

That said, this may very well be intended behavior since I'm not very
knowledgeable of the C++ standard in general, but using the libc++ debug mode,
it is reported as invalidation both in the example and with the explicit
rehash.

Reply via email to