[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
Wed Oct 23 21:03:15 PDT 2024


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

>From 8b17923a391dee3e903af4f4388f3cfe2c930708 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 dc31f31838264c..700cb51f41eeb2 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 a1b7e3b61c537566c905594d499cfce7bcae6065 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 f3d2a81d601e60a7ec33fa346b884d5ffd796881 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