[libcxx-commits] [libcxx] Add exception guard for constructor vector(n, x, a) (PR #113086)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 24 09:11:48 PDT 2024


https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/113086

>From 34b6045c8a2f2c7b5174cb5fd084c20fc3935117 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Sun, 20 Oct 2024 10:04:24 -0400
Subject: [PATCH 1/3] Add exception guard for constructor vector(n, x, a)

---
 libcxx/include/vector | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/include/vector b/libcxx/include/vector
index ce412a829ea8f7..faae84117f0e51 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -487,10 +487,12 @@ public:
   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
   vector(size_type __n, const value_type& __x, const allocator_type& __a)
       : __alloc_(__a) {
+    auto __guard = std::__make_exception_guard(__destroy_vector(*this));
     if (__n > 0) {
       __vallocate(__n);
       __construct_at_end(__n, __x);
     }
+    __guard.__complete();
   }
 
   template <class _InputIterator,

>From ed55f5388168fdfded3a0520263a4d060bf5a9dd Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 23 Oct 2024 16:19:11 -0400
Subject: [PATCH 2/3] Add an exception test to check for memory leak for
 vector(n, x, a)

---
 .../sequences/vector/vector.cons/exceptions.pass.cpp      | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
index 3ef5aeecc1b0c9..83c44bc509ba11 100644
--- a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
@@ -139,6 +139,14 @@ int main(int, char**) {
   check_new_delete_called();
 #endif  // TEST_STD_VER >= 14
 
+	try { // Throw in vector(size_type, value_type, const allocator_type&) from the type
+		int throw_after = 1;
+		ThrowingT v(throw_after); 
+    std::vector<ThrowingT> vec(1, v, std::allocator<ThrowingT>());
+  } catch (int) {
+  }
+  check_new_delete_called();
+
   try { // Throw in vector(InputIterator, InputIterator) from input iterator
     std::vector<int> vec((Iterator<std::input_iterator_tag>()), Iterator<std::input_iterator_tag>(2));
   } catch (int) {

>From 86b491c39b25fcfedbc30e003364eb6495e63ae7 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 23 Oct 2024 16:32:18 -0400
Subject: [PATCH 3/3] Addjust code indentation

---
 .../sequences/vector/vector.cons/exceptions.pass.cpp        | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
index 83c44bc509ba11..c9c1bac2fb4a0c 100644
--- a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
@@ -139,9 +139,9 @@ int main(int, char**) {
   check_new_delete_called();
 #endif  // TEST_STD_VER >= 14
 
-	try { // Throw in vector(size_type, value_type, const allocator_type&) from the type
-		int throw_after = 1;
-		ThrowingT v(throw_after); 
+  try { // Throw in vector(size_type, value_type, const allocator_type&) from the type
+    int throw_after = 1;
+    ThrowingT v(throw_after);
     std::vector<ThrowingT> vec(1, v, std::allocator<ThrowingT>());
   } catch (int) {
   }



More information about the libcxx-commits mailing list