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

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Oct 19 20:22:30 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: None (winner245)

<details>
<summary>Changes</summary>

### Overview of Changes
Added an exception guard to the `vector` 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/113069.diff


1 Files Affected:

- (modified) libcxx/include/vector (+2) 


``````````diff
diff --git a/libcxx/include/vector b/libcxx/include/vector
index dc31f31838264c..0aa4dee1bca8b3 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,

``````````

</details>


https://github.com/llvm/llvm-project/pull/113069


More information about the libcxx-commits mailing list