]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix _Safe_local_iterator<>::_M_valid_range
authorFrançois Dumont <fdumont@gcc.gnu.org>
Sun, 17 Mar 2024 16:30:58 +0000 (17:30 +0100)
committerFrançois Dumont <fdumont@gcc.gnu.org>
Wed, 20 Mar 2024 05:37:09 +0000 (06:37 +0100)
Unordered container local_iterator range shall not contain any singular
iterator unless both iterators are both value-initialized.

libstdc++-v3/ChangeLog:

* include/debug/safe_local_iterator.tcc
(_Safe_local_iterator::_M_valid_range): Add _M_value_initialized and
_M_singular checks.
* testsuite/23_containers/unordered_set/debug/114316.cc: New test case.

(cherry picked from commit 5f6e0853c30fec72d977afaa6f7a5633a8d910be)

libstdc++-v3/include/debug/safe_local_iterator.tcc
libstdc++-v3/testsuite/23_containers/unordered_set/debug/114316.cc [new file with mode: 0644]

index 6fba344f16aa424f5c1663c45cda2865ab0ccfe6..2444f41bb852abc0f2682d5ada00e4955d3156f7 100644 (file)
@@ -78,7 +78,13 @@ namespace __gnu_debug
     _M_valid_range(const _Safe_local_iterator& __rhs,
                std::pair<difference_type, _Distance_precision>& __dist) const
     {
-      if (!_M_can_compare(__rhs))
+      if (_M_value_initialized() && __rhs._M_value_initialized())
+       {
+         __dist = { 0, __dp_exact };
+         return true;
+       }
+
+      if (_M_singular() || __rhs._M_singular() || !_M_can_compare(__rhs))
        return false;
 
       if (bucket() != __rhs.bucket())
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/debug/114316.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/debug/114316.cc
new file mode 100644 (file)
index 0000000..41b649a
--- /dev/null
@@ -0,0 +1,28 @@
+// { dg-do run { target c++11 } }
+// { dg-require-debug-mode "" }
+
+// PR libstdc++/114316
+
+#include <unordered_set>
+#include <algorithm>
+
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  std::unordered_set<int>::iterator it{};
+  VERIFY( std::find(it, it, 0) == it );
+}
+
+void test02()
+{
+  std::unordered_set<int>::local_iterator it{};
+  VERIFY( std::find(it, it, 0) == it );
+}
+
+int main()
+{
+  test01();
+  test02();
+  return 0;
+}