]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: [_GLIBCXX_DEBUG] Fix assignment of value-initialized iterator [PR112477]
authorFrançois Dumont <fdumont@gcc.gnu.org>
Wed, 10 Jan 2024 18:06:48 +0000 (19:06 +0100)
committerFrançois Dumont <fdumont@gcc.gnu.org>
Thu, 11 Jan 2024 18:29:37 +0000 (19:29 +0100)
Now that _M_Detach do not reset iterator _M_version value we need to reset it when
the iterator is attached to a new sequence, even if this sequencer is null when
assigning a value-initialized iterator. In this case _M_version shall be resetted to 0.

libstdc++-v3/ChangeLog:

PR libstdc++/112477
* src/c++11/debug.cc
(_Safe_iterator_base::_M_attach): Reset _M_version to 0 if attaching to null
sequence.
(_Safe_iterator_base::_M_attach_single): Likewise.
(_Safe_local_iterator_base::_M_attach): Likewise.
(_Safe_local_iterator_base::_M_attach_single): Likewise.
* testsuite/23_containers/map/debug/112477.cc: New test case.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/src/c++11/debug.cc
libstdc++-v3/testsuite/23_containers/map/debug/112477.cc [new file with mode: 0644]

index 54092f541baf0ccf67bd72e4434be85d9da460a7..5d6bb5b7547a5490a0b11b20a530e84334ac168c 100644 (file)
@@ -437,6 +437,8 @@ namespace __gnu_debug
        _M_version = _M_sequence->_M_version;
        _M_sequence->_M_attach(this, __constant);
       }
+    else
+      _M_version = 0;
   }
 
   void
@@ -452,6 +454,8 @@ namespace __gnu_debug
        _M_version = _M_sequence->_M_version;
        _M_sequence->_M_attach_single(this, __constant);
       }
+    else
+      _M_version = 0;
   }
 
   void
@@ -528,6 +532,8 @@ namespace __gnu_debug
        _M_version = _M_sequence->_M_version;
        _M_get_container()->_M_attach_local(this, __constant);
       }
+    else
+      _M_version = 0;
   }
 
   void
@@ -543,6 +549,8 @@ namespace __gnu_debug
        _M_version = _M_sequence->_M_version;
        _M_get_container()->_M_attach_local_single(this, __constant);
       }
+    else
+      _M_version = 0;
   }
 
   void
diff --git a/libstdc++-v3/testsuite/23_containers/map/debug/112477.cc b/libstdc++-v3/testsuite/23_containers/map/debug/112477.cc
new file mode 100644 (file)
index 0000000..bde613b
--- /dev/null
@@ -0,0 +1,20 @@
+// { dg-do run { target c++11 } }
+// { dg-require-debug-mode "" }
+
+// PR libstdc++/112477
+
+#include <map>
+
+int main()
+{
+  using M = std::map<int, int>;
+  using I = M::iterator;
+
+  M map{ {1, 1}, {2, 2} };
+
+  I it1 = map.begin();
+  it1 = I{};
+
+  I it2{};
+  (void)(it1 == it2);
+}