From db02531aa6f2478a9ba8d091051f4a289f61e7fe Mon Sep 17 00:00:00 2001 From: Peng Liu 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 7889e8c2201ac..844e5d6a21056 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 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 3ef5aeecc1b0c..83c44bc509ba1 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 vec(1, v, std::allocator()); + } catch (int) { + } + check_new_delete_called(); + try { // Throw in vector(InputIterator, InputIterator) from input iterator std::vector vec((Iterator()), Iterator(2)); } catch (int) { From ef3f34c2112c7f6a85c403ccf5aa0c55852ecf2f Mon Sep 17 00:00:00 2001 From: Peng Liu 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 83c44bc509ba1..c9c1bac2fb4a0 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 vec(1, v, std::allocator()); } catch (int) { }