[libcxx-commits] [libcxx] [libc++] Assert that erase and extract are not passed end() in the associative containers (PR #232661)

Ilyas Mallah via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 16 03:04:35 PDT 2026


https://github.com/ilyas-mallah created https://github.com/llvm/llvm-project/pull/232661

`erase(end())` and `extract(end())` on map, multimap, set and multiset treat the end node as an element and crash with a SIGSEGV in the fast mode. `m.erase(m.find(k))` with a missing key is one easy way to get there. On the unordered containers, `extract(end())` reads through a null node.

This adds the `__p != end()` assertion that `__hash_table::erase` already has to `erase` in `__tree` and to `extract` in both. A range `erase` that runs past `end()` now traps too, since it loops over the single-iterator `erase`.

The tests cover `erase` and `extract` with `end()` on all eight containers, including the unordered `erase` check, which had no hardening test.

AI usage: Claude Code helped with the test runs and test scaffolding. I traced the issue, made the fix, and validated every change.


>From de8a6660363b877344d9c7261eb3bfac3419a149 Mon Sep 17 00:00:00 2001
From: Ilyas Mallah <github at ilyasm.dev>
Date: Thu, 15 Oct 2026 01:15:15 +0200
Subject: [PATCH] [libc++] Assert that erase and extract are not passed end()
 in the associative containers

---
 libcxx/include/__hash_table                   |  2 +
 libcxx/include/__tree                         |  4 ++
 .../map/assert.erase_extract.end.pass.cpp     | 59 ++++++++++++++++++
 .../set/assert.erase_extract.end.pass.cpp     | 61 +++++++++++++++++++
 .../assert.erase_extract.end.pass.cpp         | 41 +++++++++++++
 .../assert.erase_extract.end.pass.cpp         | 41 +++++++++++++
 .../assert.erase_extract.end.pass.cpp         | 42 +++++++++++++
 .../assert.erase_extract.end.pass.cpp         | 42 +++++++++++++
 8 files changed, 292 insertions(+)
 create mode 100644 libcxx/test/libcxx/containers/associative/map/assert.erase_extract.end.pass.cpp
 create mode 100644 libcxx/test/libcxx/containers/associative/set/assert.erase_extract.end.pass.cpp
 create mode 100644 libcxx/test/libcxx/containers/unord/unord.map/assert.erase_extract.end.pass.cpp
 create mode 100644 libcxx/test/libcxx/containers/unord/unord.multimap/assert.erase_extract.end.pass.cpp
 create mode 100644 libcxx/test/libcxx/containers/unord/unord.multiset/assert.erase_extract.end.pass.cpp
 create mode 100644 libcxx/test/libcxx/containers/unord/unord.set/assert.erase_extract.end.pass.cpp

diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table
index 23916e3a3ecf32..c3df10b305488f 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -1695,6 +1695,8 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(key_type const&
 template <class _Tp, class _Hash, class _Equal, class _Alloc>
 template <class _NodeHandle>
 _LIBCPP_HIDE_FROM_ABI _NodeHandle __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(const_iterator __p) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+      __p != end(), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
   allocator_type __alloc(__node_alloc());
   return _NodeHandle(remove(__p).release(), __alloc);
 }
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index 7cfa8e86fdc028..8c977d74bd53af 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -2133,6 +2133,8 @@ template <class _Tp, class _Compare, class _Allocator>
 template <class _NodeHandle>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodeHandle
 __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+      __p != end(), "associative container::extract(iterator) called with a non-dereferenceable iterator");
   __node_pointer __np = __p.__get_np();
   __remove_node_pointer(__np);
   return _NodeHandle(__np, __alloc());
@@ -2200,6 +2202,8 @@ __tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(__tree<_Tp, _Comp2,
 template <class _Tp, class _Compare, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX26
     typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+      __p != end(), "associative container::erase(iterator) called with a non-dereferenceable iterator");
   __node_pointer __np    = __p.__get_np();
   iterator __r           = __remove_node_pointer(__np);
   __node_allocator& __na = __node_alloc();
diff --git a/libcxx/test/libcxx/containers/associative/map/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/associative/map/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..c135643b88fa24
--- /dev/null
+++ b/libcxx/test/libcxx/containers/associative/map/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,59 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <map>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <map>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  {
+    std::map<int, int> c{{1, 1}, {2, 2}, {3, 3}};
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.end()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.cend()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.cend()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::map<int, int> c;
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::multimap<int, int> c{{1, 1}, {2, 2}, {3, 3}};
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.end()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.cend()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.cend()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::multimap<int, int> c;
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/containers/associative/set/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/associative/set/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..83ac0a69618508
--- /dev/null
+++ b/libcxx/test/libcxx/containers/associative/set/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <set>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <set>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  {
+    int a[] = {1, 2, 3};
+    std::set<int> c(a, a + 3);
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.end()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.cend()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.cend()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::set<int> c;
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    int a[] = {1, 2, 3};
+    std::multiset<int> c(a, a + 3);
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.end()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.cend()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.cend()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::multiset<int> c;
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/containers/unord/unord.map/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/unord/unord.map/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..560621191b1bd1
--- /dev/null
+++ b/libcxx/test/libcxx/containers/unord/unord.map/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <unordered_map>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <unordered_map>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  {
+    std::unordered_map<int, int> c{{1, 1}, {2, 2}, {3, 3}};
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.end()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.cend()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.cend()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::unordered_map<int, int> c;
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/containers/unord/unord.multimap/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/unord/unord.multimap/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..6ea3e3f04bd341
--- /dev/null
+++ b/libcxx/test/libcxx/containers/unord/unord.multimap/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <unordered_map>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <unordered_map>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  {
+    std::unordered_multimap<int, int> c{{1, 1}, {2, 2}, {3, 3}};
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.end()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.cend()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.cend()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::unordered_multimap<int, int> c;
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/containers/unord/unord.multiset/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/unord/unord.multiset/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..58c3c348e96243
--- /dev/null
+++ b/libcxx/test/libcxx/containers/unord/unord.multiset/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <unordered_set>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <unordered_set>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  {
+    int a[] = {1, 2, 3};
+    std::unordered_multiset<int> c(a, a + 3);
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.end()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.cend()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.cend()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::unordered_multiset<int> c;
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/containers/unord/unord.set/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/unord/unord.set/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..568aca8125af88
--- /dev/null
+++ b/libcxx/test/libcxx/containers/unord/unord.set/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <unordered_set>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <unordered_set>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  {
+    int a[] = {1, 2, 3};
+    std::unordered_set<int> c(a, a + 3);
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.end()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.erase(c.cend()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.cend()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  {
+    std::unordered_set<int> c;
+    TEST_LIBCPP_ASSERT_FAILURE(
+        c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+  }
+
+  return 0;
+}



More information about the libcxx-commits mailing list