[libcxx-commits] [libcxx] [libc++] Always initialize __tree::{, const_}iterator (PR #147167)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jul 5 16:27:23 PDT 2025
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/147167
There doesn't seem to be much of a reason to not initialize the iterator types in all standard modes. This avoids potential reads of uninitialized memory in C++03/C++11 without any harm I can see.
>From 40673a09e6f1ce1b3e238b8ba3e258f6f6362314 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sun, 6 Jul 2025 01:26:04 +0200
Subject: [PATCH] [libc++] Always initialize __tree::{,const_}iterator
---
libcxx/include/__tree | 14 ++--------
.../map/initialize_iterator.pass.cpp | 26 +++++++++++++++++++
.../set/initialize_iterator.pass.cpp | 26 +++++++++++++++++++
3 files changed, 54 insertions(+), 12 deletions(-)
create mode 100644 libcxx/test/libcxx/containers/associative/map/initialize_iterator.pass.cpp
create mode 100644 libcxx/test/libcxx/containers/associative/set/initialize_iterator.pass.cpp
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index 9d4ba3ad0639c..5dee314b847f2 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -646,12 +646,7 @@ public:
using reference = value_type&;
using pointer = __rebind_pointer_t<_NodePtr, value_type>;
- _LIBCPP_HIDE_FROM_ABI __tree_iterator() _NOEXCEPT
-#if _LIBCPP_STD_VER >= 14
- : __ptr_(nullptr)
-#endif
- {
- }
+ _LIBCPP_HIDE_FROM_ABI __tree_iterator() _NOEXCEPT : __ptr_(nullptr) {}
_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __get_np()->__value_; }
_LIBCPP_HIDE_FROM_ABI pointer operator->() const { return pointer_traits<pointer>::pointer_to(__get_np()->__value_); }
@@ -720,12 +715,7 @@ public:
using reference = const value_type&;
using pointer = __rebind_pointer_t<_NodePtr, const value_type>;
- _LIBCPP_HIDE_FROM_ABI __tree_const_iterator() _NOEXCEPT
-#if _LIBCPP_STD_VER >= 14
- : __ptr_(nullptr)
-#endif
- {
- }
+ _LIBCPP_HIDE_FROM_ABI __tree_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
private:
typedef __tree_iterator<_Tp, __node_pointer, difference_type> __non_const_iterator;
diff --git a/libcxx/test/libcxx/containers/associative/map/initialize_iterator.pass.cpp b/libcxx/test/libcxx/containers/associative/map/initialize_iterator.pass.cpp
new file mode 100644
index 0000000000000..cfa8e72e98e51
--- /dev/null
+++ b/libcxx/test/libcxx/containers/associative/map/initialize_iterator.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// Check that map::iterator is initialized when default constructed
+
+#include <cassert>
+#include <map>
+
+template <class Iter>
+void test() {
+ Iter iter;
+ Iter iter2 = Iter();
+ assert(iter == iter2);
+}
+
+int main() {
+ test<std::map<int, int>::iterator>();
+ test<std::map<int, int>::const_iterator>();
+}
diff --git a/libcxx/test/libcxx/containers/associative/set/initialize_iterator.pass.cpp b/libcxx/test/libcxx/containers/associative/set/initialize_iterator.pass.cpp
new file mode 100644
index 0000000000000..c919e5c3680c3
--- /dev/null
+++ b/libcxx/test/libcxx/containers/associative/set/initialize_iterator.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// Check that set::iterator is initialized when default constructed
+
+#include <cassert>
+#include <set>
+
+template <class Iter>
+void test() {
+ Iter iter;
+ Iter iter2 = Iter();
+ assert(iter == iter2);
+}
+
+int main() {
+ test<std::set<int>::iterator>();
+ test<std::set<int>::const_iterator>();
+}
More information about the libcxx-commits
mailing list