Skip to content

Commit c5cd1e9

Browse files
authored
[libc++] Add exception guard for vector<bool>::__init_with_sentinel (#115491)
As a drive-by, also improve the test coverage for throwing exceptions in vector<bool> constructors.
1 parent 07a8ebe commit c5cd1e9

File tree

3 files changed

+162
-83
lines changed

3 files changed

+162
-83
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,12 @@ class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator> {
390390
template <class _InputIterator, class _Sentinel>
391391
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
392392
__init_with_sentinel(_InputIterator __first, _Sentinel __last) {
393-
#if _LIBCPP_HAS_EXCEPTIONS
394-
try {
395-
#endif // _LIBCPP_HAS_EXCEPTIONS
396-
for (; __first != __last; ++__first)
397-
push_back(*__first);
398-
#if _LIBCPP_HAS_EXCEPTIONS
399-
} catch (...) {
400-
if (__begin_ != nullptr)
401-
__storage_traits::deallocate(__alloc_, __begin_, __cap_);
402-
throw;
403-
}
404-
#endif // _LIBCPP_HAS_EXCEPTIONS
393+
auto __guard = std::__make_exception_guard(__destroy_vector(*this));
394+
395+
for (; __first != __last; ++__first)
396+
push_back(*__first);
397+
398+
__guard.__complete();
405399
}
406400

407401
template <class _Iterator, class _Sentinel>

libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp

Lines changed: 59 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,133 +12,121 @@
1212
// Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception
1313

1414
#include <cstddef>
15-
#include <type_traits>
1615
#include <memory>
16+
#include <type_traits>
1717
#include <vector>
1818

19+
#include "../vector/common.h"
1920
#include "count_new.h"
2021
#include "test_iterators.h"
2122

22-
template <class T>
23-
struct Allocator {
24-
using value_type = T;
25-
using is_always_equal = std::false_type;
26-
27-
template <class U>
28-
Allocator(const Allocator<U>&) {}
29-
30-
Allocator(bool should_throw = true) {
31-
if (should_throw)
32-
throw 0;
33-
}
34-
35-
T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
36-
void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
37-
38-
template <class U>
39-
friend bool operator==(const Allocator&, const Allocator<U>&) { return true; }
40-
};
41-
42-
template <class IterCat>
43-
struct Iterator {
44-
using iterator_category = IterCat;
45-
using difference_type = std::ptrdiff_t;
46-
using value_type = bool;
47-
using reference = bool&;
48-
using pointer = bool*;
49-
50-
int i_;
51-
bool b_ = true;
52-
Iterator(int i = 0) : i_(i) {}
53-
bool& operator*() {
54-
if (i_ == 1)
55-
throw 1;
56-
return b_;
57-
}
58-
59-
friend bool operator==(const Iterator& lhs, const Iterator& rhs) { return lhs.i_ == rhs.i_; }
60-
61-
friend bool operator!=(const Iterator& lhs, const Iterator& rhs) { return lhs.i_ != rhs.i_; }
62-
63-
Iterator& operator++() {
64-
++i_;
65-
return *this;
66-
}
23+
int main(int, char**) {
24+
using AllocVec = std::vector<bool, throwing_allocator<bool> >;
6725

68-
Iterator operator++(int) {
69-
auto tmp = *this;
70-
++i_;
71-
return tmp;
26+
try { // Throw in vector() from allocator
27+
AllocVec vec;
28+
} catch (int) {
7229
}
73-
};
74-
75-
void check_new_delete_called() {
76-
assert(globalMemCounter.new_called == globalMemCounter.delete_called);
77-
assert(globalMemCounter.new_array_called == globalMemCounter.delete_array_called);
78-
assert(globalMemCounter.aligned_new_called == globalMemCounter.aligned_delete_called);
79-
assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
80-
}
81-
82-
int main(int, char**) {
83-
using AllocVec = std::vector<bool, Allocator<bool> >;
30+
check_new_delete_called();
8431

8532
#if TEST_STD_VER >= 14
8633
try { // Throw in vector(size_type, const allocator_type&) from allocator
87-
Allocator<bool> alloc(false);
34+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
8835
AllocVec get_alloc(0, alloc);
8936
} catch (int) {
9037
}
9138
check_new_delete_called();
92-
#endif // TEST_STD_VER >= 14
39+
#endif // TEST_STD_VER >= 14
40+
41+
try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator
42+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
43+
AllocVec get_alloc(0, true, alloc);
44+
} catch (int) {
45+
}
46+
check_new_delete_called();
9347

9448
try { // Throw in vector(InputIterator, InputIterator) from input iterator
95-
std::vector<bool> vec((Iterator<std::input_iterator_tag>()), Iterator<std::input_iterator_tag>(2));
49+
std::vector<bool> vec(
50+
throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2));
9651
} catch (int) {
9752
}
9853
check_new_delete_called();
9954

10055
try { // Throw in vector(InputIterator, InputIterator) from forward iterator
101-
std::vector<bool> vec((Iterator<std::forward_iterator_tag>()), Iterator<std::forward_iterator_tag>(2));
56+
std::vector<bool> vec(
57+
throwing_iterator<bool, std::forward_iterator_tag>(), throwing_iterator<bool, std::forward_iterator_tag>(2));
10258
} catch (int) {
10359
}
10460
check_new_delete_called();
10561

10662
try { // Throw in vector(InputIterator, InputIterator) from allocator
107-
int a[] = {1, 2};
108-
AllocVec vec(cpp17_input_iterator<int*>(a), cpp17_input_iterator<int*>(a + 2));
63+
bool a[] = {true, true};
64+
AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2));
10965
} catch (int) {
11066
}
11167
check_new_delete_called();
11268

11369
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
11470
std::allocator<bool> alloc;
115-
std::vector<bool> vec(Iterator<std::input_iterator_tag>(), Iterator<std::input_iterator_tag>(2), alloc);
71+
std::vector<bool> vec(
72+
throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc);
11673
} catch (int) {
11774
}
11875
check_new_delete_called();
11976

12077
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
12178
std::allocator<bool> alloc;
122-
std::vector<bool> vec(Iterator<std::forward_iterator_tag>(), Iterator<std::forward_iterator_tag>(2), alloc);
79+
std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(),
80+
throwing_iterator<bool, std::forward_iterator_tag>(2),
81+
alloc);
12382
} catch (int) {
12483
}
12584
check_new_delete_called();
12685

12786
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
12887
bool a[] = {true, false};
129-
Allocator<bool> alloc(false);
88+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
13089
AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2), alloc);
13190
} catch (int) {
13291
}
13392
check_new_delete_called();
13493

13594
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
13695
bool a[] = {true, false};
137-
Allocator<bool> alloc(false);
96+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
13897
AllocVec vec(forward_iterator<bool*>(a), forward_iterator<bool*>(a + 2), alloc);
13998
} catch (int) {
14099
}
141100
check_new_delete_called();
142101

102+
#if TEST_STD_VER >= 11
103+
try { // Throw in vector(const vector&, const allocator_type&) from allocator
104+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
105+
AllocVec vec(alloc);
106+
vec.push_back(true);
107+
alloc.throw_on_copy_ = true;
108+
AllocVec vec2(vec, alloc);
109+
} catch (int) {
110+
}
111+
check_new_delete_called();
112+
113+
try { // Throw in vector(vector&&, const allocator_type&) from allocator
114+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
115+
AllocVec vec(alloc);
116+
vec.push_back(true);
117+
alloc.throw_on_copy_ = true;
118+
AllocVec vec2(std::move(vec), alloc);
119+
} catch (int) {
120+
}
121+
check_new_delete_called();
122+
123+
try { // Throw in vector(initializer_list<value_type>, const allocator_type&) constructor from allocator
124+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
125+
AllocVec vec({true, true}, alloc);
126+
} catch (int) {
127+
}
128+
check_new_delete_called();
129+
#endif // TEST_STD_VER >= 11
130+
143131
return 0;
144132
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef TEST_STD_CONTAINERS_SEQUENCES_VECTOR_COMMON_H
10+
#define TEST_STD_CONTAINERS_SEQUENCES_VECTOR_COMMON_H
11+
12+
#include <cassert>
13+
#include <cstddef>
14+
#include <memory>
15+
#include <type_traits>
16+
17+
#include "count_new.h"
18+
19+
template <class T>
20+
struct throwing_allocator {
21+
using value_type = T;
22+
using is_always_equal = std::false_type;
23+
24+
bool throw_on_copy_ = false;
25+
26+
explicit throwing_allocator(bool throw_on_ctor = true) {
27+
if (throw_on_ctor)
28+
throw 0;
29+
}
30+
31+
explicit throwing_allocator(bool throw_on_ctor, bool throw_on_copy) : throw_on_copy_(throw_on_copy) {
32+
if (throw_on_ctor)
33+
throw 0;
34+
}
35+
36+
throwing_allocator(const throwing_allocator& rhs) : throw_on_copy_(rhs.throw_on_copy_) {
37+
if (throw_on_copy_)
38+
throw 0;
39+
}
40+
41+
template <class U>
42+
throwing_allocator(const throwing_allocator<U>& rhs) : throw_on_copy_(rhs.throw_on_copy_) {
43+
if (throw_on_copy_)
44+
throw 0;
45+
}
46+
47+
T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
48+
void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
49+
50+
template <class U>
51+
friend bool operator==(const throwing_allocator&, const throwing_allocator<U>&) {
52+
return true;
53+
}
54+
};
55+
56+
template <class T, class IterCat>
57+
struct throwing_iterator {
58+
using iterator_category = IterCat;
59+
using difference_type = std::ptrdiff_t;
60+
using value_type = T;
61+
using reference = T&;
62+
using pointer = T*;
63+
64+
int i_;
65+
T v_;
66+
67+
throwing_iterator(int i = 0, const T& v = T()) : i_(i), v_(v) {}
68+
69+
reference operator*() {
70+
if (i_ == 1)
71+
throw 1;
72+
return v_;
73+
}
74+
75+
friend bool operator==(const throwing_iterator& lhs, const throwing_iterator& rhs) { return lhs.i_ == rhs.i_; }
76+
friend bool operator!=(const throwing_iterator& lhs, const throwing_iterator& rhs) { return lhs.i_ != rhs.i_; }
77+
78+
throwing_iterator& operator++() {
79+
++i_;
80+
return *this;
81+
}
82+
83+
throwing_iterator operator++(int) {
84+
auto tmp = *this;
85+
++i_;
86+
return tmp;
87+
}
88+
};
89+
90+
inline void check_new_delete_called() {
91+
assert(globalMemCounter.new_called == globalMemCounter.delete_called);
92+
assert(globalMemCounter.new_array_called == globalMemCounter.delete_array_called);
93+
assert(globalMemCounter.aligned_new_called == globalMemCounter.aligned_delete_called);
94+
assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
95+
}
96+
97+
#endif // TEST_STD_CONTAINERS_SEQUENCES_VECTOR_COMMON_H

0 commit comments

Comments
 (0)