[libcxx-commits] [libcxx] [libc++] Add exception guard for vector<bool>::__init_with_sentinel (PR #115491)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Nov 13 07:17:14 PST 2024
================
@@ -398,18 +398,12 @@ class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator> {
template <class _InputIterator, class _Sentinel>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
__init_with_sentinel(_InputIterator __first, _Sentinel __last) {
-#if _LIBCPP_HAS_EXCEPTIONS
- try {
-#endif // _LIBCPP_HAS_EXCEPTIONS
- for (; __first != __last; ++__first)
- push_back(*__first);
-#if _LIBCPP_HAS_EXCEPTIONS
- } catch (...) {
- if (__begin_ != nullptr)
- __storage_traits::deallocate(__alloc(), __begin_, __cap());
- throw;
- }
-#endif // _LIBCPP_HAS_EXCEPTIONS
+ auto __guard = std::__make_exception_guard(__destroy_vector(*this));
----------------
winner245 wrote:
I have added an exception test file for `vector<bool>`, which covers all constructors that might raise exceptions in various scenarios. Since the exception tests for `vector<bool>` and `vector<T>` are similar, I factored out the common code used by both tests into a header file `exception_test_helpers.h`.
Additionally, I found some issues with the existing exceptions tests for `vector<T>`: some tests did not throw exception as intended, making them not useful, and some tests did not throw exceptions at the right points, failing to fulfill their purpose. We may need another PR to fix that.
https://github.com/llvm/llvm-project/pull/115491
More information about the libcxx-commits
mailing list