Skip to content

Conversation

@winner245
Copy link
Contributor

Overview of Changes

Added exception guard to the vector(n, x, a) constructor to enhance exception safety.

Details

This change ensures that the vector(n, x, a) constructor is consistent with other constructors, such as vector(n, x) and vector(n, a), in terms of exception safety.

Impact

Improves robustness and resource management during exceptions.

@winner245 winner245 requested a review from a team as a code owner October 20, 2024 03:44
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Oct 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 20, 2024

@llvm/pr-subscribers-libcxx

Author: None (winner245)

Changes

Overview of Changes

Added exception guard to the vector(n, x, a) constructor to enhance exception safety.

Details

This change ensures that the vector(n, x, a) constructor is consistent with other constructors, such as vector(n, x) and vector(n, a), in terms of exception safety.

Impact

Improves robustness and resource management during exceptions.


Full diff: https://github.com/llvm/llvm-project/pull/113070.diff

1 Files Affected:

  • (modified) libcxx/include/vector (+2)
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,

@winner245 winner245 closed this Oct 20, 2024
@winner245 winner245 deleted the winner245/dev branch October 20, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants