[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
Mon Oct 28 10:16:52 PDT 2024
https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/113086
>From db02531aa6f2478a9ba8d091051f4a289f61e7fe 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/vector.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 7889e8c2201ac1..844e5d6a210568 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -165,10 +165,12 @@ class _LIBCPP_TEMPLATE_VIS vector {
_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 bd6dd328f4c4322a990b94283e72a8676b15cbdc 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 ef3f34c2112c7f6a85c403ccf5aa0c55852ecf2f 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