[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
Tue Nov 26 16:50:39 PST 2024
https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/115491
>From 5cbf13c3dede84d05e30ede1e14a95cbe58fafdc Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 13 Nov 2024 09:45:01 -0500
Subject: [PATCH 1/6] Add exception tests for vector<bool>
---
.../sequences/vector.bool/exceptions.pass.cpp | 124 ++++++++++++++++++
libcxx/test/support/exception_test_helpers.h | 80 +++++++++++
2 files changed, 204 insertions(+)
create mode 100644 libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
create mode 100644 libcxx/test/support/exception_test_helpers.h
diff --git a/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
new file mode 100644
index 00000000000000..c6c27dfec59ea3
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
@@ -0,0 +1,124 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: no-exceptions
+
+// Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception
+
+#include <cstddef>
+#include <memory>
+#include <type_traits>
+#include <vector>
+
+#include "exception_test_helpers.h"
+#include "test_iterators.h"
+
+int main(int, char**) {
+ using AllocVec = std::vector<bool, throwing_allocator<bool> >;
+ try { // Throw in vector() from allocator
+ AllocVec vec; // Throw on default construction
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+#if TEST_STD_VER >= 14
+ try { // Throw in vector(size_type, const allocator_type&) from allocator
+ throwing_allocator<bool> alloc(false, true); // Throw on copy only
+ AllocVec get_alloc(0, alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+#endif // TEST_STD_VER >= 14
+
+ try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator
+ throwing_allocator<bool> alloc(false, true); // Throw on copy only
+ AllocVec get_alloc(0, true, alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(InputIterator, InputIterator) from input iterator
+ std::vector<bool> vec((throwing_iterator<bool, std::input_iterator_tag>()), throwing_iterator<bool, std::input_iterator_tag>(2));
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(InputIterator, InputIterator) from forward iterator
+ std::vector<bool> vec((throwing_iterator<bool, std::forward_iterator_tag>()), throwing_iterator<bool, std::forward_iterator_tag>(2));
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(InputIterator, InputIterator) from allocator
+ bool a[] = {true, true};
+ AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2)); // throwing_allocator throws on default construction
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
+ std::allocator<bool> alloc;
+ std::vector<bool> vec(throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
+ std::allocator<bool> alloc;
+ std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(), throwing_iterator<bool, std::forward_iterator_tag>(2), alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
+ bool a[] = {true, true};
+ throwing_allocator<bool> alloc(false, true); // Throw on copy only
+ AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2), alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
+ bool a[] = {true, true};
+ throwing_allocator<bool> alloc(false, true); // Throw on copy only
+ AllocVec vec(forward_iterator<bool*>(a), forward_iterator<bool*>(a + 2), alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+#if TEST_STD_VER >= 11
+ try { // Throw in vector(const vector&, const allocator_type&) from allocator
+ throwing_allocator<bool> alloc(false, false);
+ AllocVec vec(alloc);
+ vec.emplace_back(true);
+ alloc.throw_on_copy_ = true;
+ AllocVec vec2(vec, alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(vector&&, const allocator_type&) from allocator
+ throwing_allocator<bool> alloc(false, false);
+ AllocVec vec(alloc);
+ vec.emplace_back(true);
+ alloc.throw_on_copy_ = true;
+ AllocVec vec2(std::move(vec), alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(initializer_list<value_type>, const allocator_type&) constructor from allocator
+ throwing_allocator<bool> alloc(false, true); // Throw on copy only
+ AllocVec vec({true, true}, alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+#endif // TEST_STD_VER >= 11
+
+ return 0;
+}
diff --git a/libcxx/test/support/exception_test_helpers.h b/libcxx/test/support/exception_test_helpers.h
new file mode 100644
index 00000000000000..4f680e9486e0a5
--- /dev/null
+++ b/libcxx/test/support/exception_test_helpers.h
@@ -0,0 +1,80 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef VECTOR_EXCEPTION_TEST_HELPER_H
+#define VECTOR_EXCEPTION_TEST_HELPER_H
+
+#include "count_new.h"
+
+template <class T>
+struct throwing_allocator {
+ using value_type = T;
+ using is_always_equal = std::false_type;
+
+ bool throw_on_copy_ = false;
+
+ throwing_allocator(bool throw_on_ctor = true, bool throw_on_copy = false) : throw_on_copy_(throw_on_copy) {
+ if (throw_on_ctor)
+ throw 0;
+ }
+
+ template <class U>
+ throwing_allocator(const throwing_allocator<U>& rhs) : throw_on_copy_(rhs.throw_on_copy_) {
+ if (throw_on_copy_)
+ throw 0;
+ }
+
+ T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+ void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
+
+ template <class U>
+ friend bool operator==(const throwing_allocator&, const throwing_allocator<U>&) { return true; }
+};
+
+template <class T, class IterCat>
+struct throwing_iterator {
+ using iterator_category = IterCat;
+ using difference_type = std::ptrdiff_t;
+ using value_type = T;
+ using reference = T&;
+ using pointer = T*;
+
+ int i_;
+ T v_;
+
+ throwing_iterator(int i = 0, const T& v = T()) : i_(i), v_(v) {}
+
+ reference operator*() {
+ if (i_ == 1)
+ throw 1;
+ return v_;
+ }
+
+ friend bool operator==(const throwing_iterator& lhs, const throwing_iterator& rhs) { return lhs.i_ == rhs.i_; }
+ friend bool operator!=(const throwing_iterator& lhs, const throwing_iterator& rhs) { return lhs.i_ != rhs.i_; }
+
+ throwing_iterator& operator++() {
+ ++i_;
+ return *this;
+ }
+
+ throwing_iterator operator++(int) {
+ auto tmp = *this;
+ ++i_;
+ return tmp;
+ }
+};
+
+inline void check_new_delete_called() {
+ assert(globalMemCounter.new_called == globalMemCounter.delete_called);
+ assert(globalMemCounter.new_array_called == globalMemCounter.delete_array_called);
+ assert(globalMemCounter.aligned_new_called == globalMemCounter.aligned_delete_called);
+ assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
+}
+
+#endif // VECTOR_EXCEPTION_TEST_HELPER_H
\ No newline at end of file
>From 6cf147d1b87b96b91692797f00c8df4b009909d6 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Fri, 8 Nov 2024 00:23:42 -0500
Subject: [PATCH 2/6] Add exception guard for
vector<bool>::__init_with_sentinel
---
libcxx/include/__vector/vector_bool.h | 18 ++++++------------
1 file changed, 6 insertions(+), 12 deletions(-)
diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 3b03f65b7ee549..36eb7f350ac406 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -390,18 +390,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));
+
+ for (; __first != __last; ++__first)
+ push_back(*__first);
+
+ __guard.__complete();
}
template <class _Iterator, class _Sentinel>
>From 2af5cf258d4456aa532a4fb7ff7396d4f127d2b1 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 13 Nov 2024 10:01:30 -0500
Subject: [PATCH 3/6] Run clang-format
---
.../sequences/vector.bool/exceptions.pass.cpp | 26 ++++++++++++-------
libcxx/test/support/exception_test_helpers.h | 6 +++--
2 files changed, 20 insertions(+), 12 deletions(-)
diff --git a/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
index c6c27dfec59ea3..a6f9488347a5c0 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
@@ -20,20 +20,20 @@
int main(int, char**) {
using AllocVec = std::vector<bool, throwing_allocator<bool> >;
- try { // Throw in vector() from allocator
+ try { // Throw in vector() from allocator
AllocVec vec; // Throw on default construction
} catch (int) {
}
check_new_delete_called();
-#if TEST_STD_VER >= 14
- try { // Throw in vector(size_type, const allocator_type&) from allocator
+#if TEST_STD_VER >= 14
+ try { // Throw in vector(size_type, const allocator_type&) from allocator
throwing_allocator<bool> alloc(false, true); // Throw on copy only
AllocVec get_alloc(0, alloc);
} catch (int) {
}
check_new_delete_called();
-#endif // TEST_STD_VER >= 14
+#endif // TEST_STD_VER >= 14
try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator
throwing_allocator<bool> alloc(false, true); // Throw on copy only
@@ -43,34 +43,40 @@ int main(int, char**) {
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator) from input iterator
- std::vector<bool> vec((throwing_iterator<bool, std::input_iterator_tag>()), throwing_iterator<bool, std::input_iterator_tag>(2));
+ std::vector<bool> vec(
+ (throwing_iterator<bool, std::input_iterator_tag>()), throwing_iterator<bool, std::input_iterator_tag>(2));
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator) from forward iterator
- std::vector<bool> vec((throwing_iterator<bool, std::forward_iterator_tag>()), throwing_iterator<bool, std::forward_iterator_tag>(2));
+ std::vector<bool> vec(
+ (throwing_iterator<bool, std::forward_iterator_tag>()), throwing_iterator<bool, std::forward_iterator_tag>(2));
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator) from allocator
bool a[] = {true, true};
- AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2)); // throwing_allocator throws on default construction
+ AllocVec vec(cpp17_input_iterator<bool*>(a),
+ cpp17_input_iterator<bool*>(a + 2)); // throwing_allocator throws on default construction
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
std::allocator<bool> alloc;
- std::vector<bool> vec(throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc);
+ std::vector<bool> vec(
+ throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc);
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
std::allocator<bool> alloc;
- std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(), throwing_iterator<bool, std::forward_iterator_tag>(2), alloc);
+ std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(),
+ throwing_iterator<bool, std::forward_iterator_tag>(2),
+ alloc);
} catch (int) {
}
check_new_delete_called();
@@ -79,7 +85,7 @@ int main(int, char**) {
bool a[] = {true, true};
throwing_allocator<bool> alloc(false, true); // Throw on copy only
AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2), alloc);
- } catch (int) {
+ } catch (int) {
}
check_new_delete_called();
diff --git a/libcxx/test/support/exception_test_helpers.h b/libcxx/test/support/exception_test_helpers.h
index 4f680e9486e0a5..44be6916cb0226 100644
--- a/libcxx/test/support/exception_test_helpers.h
+++ b/libcxx/test/support/exception_test_helpers.h
@@ -16,7 +16,7 @@ struct throwing_allocator {
using value_type = T;
using is_always_equal = std::false_type;
- bool throw_on_copy_ = false;
+ bool throw_on_copy_ = false;
throwing_allocator(bool throw_on_ctor = true, bool throw_on_copy = false) : throw_on_copy_(throw_on_copy) {
if (throw_on_ctor)
@@ -33,7 +33,9 @@ struct throwing_allocator {
void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
template <class U>
- friend bool operator==(const throwing_allocator&, const throwing_allocator<U>&) { return true; }
+ friend bool operator==(const throwing_allocator&, const throwing_allocator<U>&) {
+ return true;
+ }
};
template <class T, class IterCat>
>From 52be4bd7c99d56494996a3979f681b5a80231b37 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 13 Nov 2024 10:20:06 -0500
Subject: [PATCH 4/6] Update header guard to match the header name
---
libcxx/test/support/exception_test_helpers.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libcxx/test/support/exception_test_helpers.h b/libcxx/test/support/exception_test_helpers.h
index 44be6916cb0226..96cf407a94ae4e 100644
--- a/libcxx/test/support/exception_test_helpers.h
+++ b/libcxx/test/support/exception_test_helpers.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef VECTOR_EXCEPTION_TEST_HELPER_H
-#define VECTOR_EXCEPTION_TEST_HELPER_H
+#ifndef EXCEPTION_TEST_HELPER_H
+#define EXCEPTION_TEST_HELPER_H
#include "count_new.h"
@@ -79,4 +79,4 @@ inline void check_new_delete_called() {
assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
}
-#endif // VECTOR_EXCEPTION_TEST_HELPER_H
\ No newline at end of file
+#endif // EXCEPTION_TEST_HELPER_H
\ No newline at end of file
>From 39eb403773078a3ee3e73d0e50afef8bfb28278a Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 13 Nov 2024 11:33:08 -0500
Subject: [PATCH 5/6] Fix compatibility
---
.../sequences/vector.bool/exceptions.pass.cpp | 4 ++--
libcxx/test/support/exception_test_helpers.h | 11 ++++++++---
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
index a6f9488347a5c0..49a20b8b7f8961 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
@@ -101,7 +101,7 @@ int main(int, char**) {
try { // Throw in vector(const vector&, const allocator_type&) from allocator
throwing_allocator<bool> alloc(false, false);
AllocVec vec(alloc);
- vec.emplace_back(true);
+ vec.push_back(true);
alloc.throw_on_copy_ = true;
AllocVec vec2(vec, alloc);
} catch (int) {
@@ -111,7 +111,7 @@ int main(int, char**) {
try { // Throw in vector(vector&&, const allocator_type&) from allocator
throwing_allocator<bool> alloc(false, false);
AllocVec vec(alloc);
- vec.emplace_back(true);
+ vec.push_back(true);
alloc.throw_on_copy_ = true;
AllocVec vec2(std::move(vec), alloc);
} catch (int) {
diff --git a/libcxx/test/support/exception_test_helpers.h b/libcxx/test/support/exception_test_helpers.h
index 96cf407a94ae4e..00a9e54331d0ab 100644
--- a/libcxx/test/support/exception_test_helpers.h
+++ b/libcxx/test/support/exception_test_helpers.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef EXCEPTION_TEST_HELPER_H
-#define EXCEPTION_TEST_HELPER_H
+#ifndef EXCEPTION_TEST_HELPERS_H
+#define EXCEPTION_TEST_HELPERS_H
#include "count_new.h"
@@ -23,6 +23,11 @@ struct throwing_allocator {
throw 0;
}
+ throwing_allocator(const throwing_allocator& rhs) : throw_on_copy_(rhs.throw_on_copy_) {
+ if (throw_on_copy_)
+ throw 0;
+ }
+
template <class U>
throwing_allocator(const throwing_allocator<U>& rhs) : throw_on_copy_(rhs.throw_on_copy_) {
if (throw_on_copy_)
@@ -79,4 +84,4 @@ inline void check_new_delete_called() {
assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
}
-#endif // EXCEPTION_TEST_HELPER_H
\ No newline at end of file
+#endif // EXCEPTION_TEST_HELPERS_H
\ No newline at end of file
>From 7c1d753edb4413a2bb5113f0ce5023b10dbbfb97 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Tue, 26 Nov 2024 19:30:17 -0500
Subject: [PATCH 6/6] Refactor in a better way
---
.../vector.bool/ctor_exceptions.pass.cpp | 130 ++++++++----------
.../sequences/vector.bool/exceptions.pass.cpp | 130 ------------------
.../containers/sequences/vector/common.h} | 15 +-
3 files changed, 70 insertions(+), 205 deletions(-)
delete mode 100644 libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
rename libcxx/test/{support/exception_test_helpers.h => std/containers/sequences/vector/common.h} (87%)
diff --git a/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp
index 928fceab1f078c..cb2b7d124edc19 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp
@@ -12,121 +12,80 @@
// Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception
#include <cstddef>
-#include <type_traits>
#include <memory>
+#include <type_traits>
#include <vector>
+#include "../vector/common.h"
#include "count_new.h"
#include "test_iterators.h"
-template <class T>
-struct Allocator {
- using value_type = T;
- using is_always_equal = std::false_type;
-
- template <class U>
- Allocator(const Allocator<U>&) {}
-
- Allocator(bool should_throw = true) {
- if (should_throw)
- throw 0;
- }
-
- T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
- void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
-
- template <class U>
- friend bool operator==(const Allocator&, const Allocator<U>&) { return true; }
-};
-
-template <class IterCat>
-struct Iterator {
- using iterator_category = IterCat;
- using difference_type = std::ptrdiff_t;
- using value_type = bool;
- using reference = bool&;
- using pointer = bool*;
-
- int i_;
- bool b_ = true;
- Iterator(int i = 0) : i_(i) {}
- bool& operator*() {
- if (i_ == 1)
- throw 1;
- return b_;
- }
-
- friend bool operator==(const Iterator& lhs, const Iterator& rhs) { return lhs.i_ == rhs.i_; }
-
- friend bool operator!=(const Iterator& lhs, const Iterator& rhs) { return lhs.i_ != rhs.i_; }
-
- Iterator& operator++() {
- ++i_;
- return *this;
- }
+int main(int, char**) {
+ using AllocVec = std::vector<bool, throwing_allocator<bool> >;
- Iterator operator++(int) {
- auto tmp = *this;
- ++i_;
- return tmp;
+ try { // Throw in vector() from allocator
+ AllocVec vec;
+ } catch (int) {
}
-};
-
-void check_new_delete_called() {
- assert(globalMemCounter.new_called == globalMemCounter.delete_called);
- assert(globalMemCounter.new_array_called == globalMemCounter.delete_array_called);
- assert(globalMemCounter.aligned_new_called == globalMemCounter.aligned_delete_called);
- assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
-}
-
-int main(int, char**) {
- using AllocVec = std::vector<bool, Allocator<bool> >;
+ check_new_delete_called();
#if TEST_STD_VER >= 14
try { // Throw in vector(size_type, const allocator_type&) from allocator
- Allocator<bool> alloc(false);
+ throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
AllocVec get_alloc(0, alloc);
} catch (int) {
}
check_new_delete_called();
-#endif // TEST_STD_VER >= 14
+#endif // TEST_STD_VER >= 14
+
+ try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator
+ throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
+ AllocVec get_alloc(0, true, alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator) from input iterator
- std::vector<bool> vec((Iterator<std::input_iterator_tag>()), Iterator<std::input_iterator_tag>(2));
+ std::vector<bool> vec(
+ throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2));
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator) from forward iterator
- std::vector<bool> vec((Iterator<std::forward_iterator_tag>()), Iterator<std::forward_iterator_tag>(2));
+ std::vector<bool> vec(
+ throwing_iterator<bool, std::forward_iterator_tag>(), throwing_iterator<bool, std::forward_iterator_tag>(2));
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator) from allocator
- int a[] = {1, 2};
- AllocVec vec(cpp17_input_iterator<int*>(a), cpp17_input_iterator<int*>(a + 2));
+ bool a[] = {true, true};
+ AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2));
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
std::allocator<bool> alloc;
- std::vector<bool> vec(Iterator<std::input_iterator_tag>(), Iterator<std::input_iterator_tag>(2), alloc);
+ std::vector<bool> vec(
+ throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc);
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
std::allocator<bool> alloc;
- std::vector<bool> vec(Iterator<std::forward_iterator_tag>(), Iterator<std::forward_iterator_tag>(2), alloc);
+ std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(),
+ throwing_iterator<bool, std::forward_iterator_tag>(2),
+ alloc);
} catch (int) {
}
check_new_delete_called();
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
bool a[] = {true, false};
- Allocator<bool> alloc(false);
+ throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2), alloc);
} catch (int) {
}
@@ -134,11 +93,40 @@ int main(int, char**) {
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
bool a[] = {true, false};
- Allocator<bool> alloc(false);
+ throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
AllocVec vec(forward_iterator<bool*>(a), forward_iterator<bool*>(a + 2), alloc);
} catch (int) {
}
check_new_delete_called();
+#if TEST_STD_VER >= 11
+ try { // Throw in vector(const vector&, const allocator_type&) from allocator
+ throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
+ AllocVec vec(alloc);
+ vec.push_back(true);
+ alloc.throw_on_copy_ = true;
+ AllocVec vec2(vec, alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(vector&&, const allocator_type&) from allocator
+ throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
+ AllocVec vec(alloc);
+ vec.push_back(true);
+ alloc.throw_on_copy_ = true;
+ AllocVec vec2(std::move(vec), alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+
+ try { // Throw in vector(initializer_list<value_type>, const allocator_type&) constructor from allocator
+ throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
+ AllocVec vec({true, true}, alloc);
+ } catch (int) {
+ }
+ check_new_delete_called();
+#endif // TEST_STD_VER >= 11
+
return 0;
}
diff --git a/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
deleted file mode 100644
index 49a20b8b7f8961..00000000000000
--- a/libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp
+++ /dev/null
@@ -1,130 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: no-exceptions
-
-// Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception
-
-#include <cstddef>
-#include <memory>
-#include <type_traits>
-#include <vector>
-
-#include "exception_test_helpers.h"
-#include "test_iterators.h"
-
-int main(int, char**) {
- using AllocVec = std::vector<bool, throwing_allocator<bool> >;
- try { // Throw in vector() from allocator
- AllocVec vec; // Throw on default construction
- } catch (int) {
- }
- check_new_delete_called();
-
-#if TEST_STD_VER >= 14
- try { // Throw in vector(size_type, const allocator_type&) from allocator
- throwing_allocator<bool> alloc(false, true); // Throw on copy only
- AllocVec get_alloc(0, alloc);
- } catch (int) {
- }
- check_new_delete_called();
-#endif // TEST_STD_VER >= 14
-
- try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator
- throwing_allocator<bool> alloc(false, true); // Throw on copy only
- AllocVec get_alloc(0, true, alloc);
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(InputIterator, InputIterator) from input iterator
- std::vector<bool> vec(
- (throwing_iterator<bool, std::input_iterator_tag>()), throwing_iterator<bool, std::input_iterator_tag>(2));
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(InputIterator, InputIterator) from forward iterator
- std::vector<bool> vec(
- (throwing_iterator<bool, std::forward_iterator_tag>()), throwing_iterator<bool, std::forward_iterator_tag>(2));
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(InputIterator, InputIterator) from allocator
- bool a[] = {true, true};
- AllocVec vec(cpp17_input_iterator<bool*>(a),
- cpp17_input_iterator<bool*>(a + 2)); // throwing_allocator throws on default construction
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
- std::allocator<bool> alloc;
- std::vector<bool> vec(
- throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc);
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
- std::allocator<bool> alloc;
- std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(),
- throwing_iterator<bool, std::forward_iterator_tag>(2),
- alloc);
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
- bool a[] = {true, true};
- throwing_allocator<bool> alloc(false, true); // Throw on copy only
- AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2), alloc);
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
- bool a[] = {true, true};
- throwing_allocator<bool> alloc(false, true); // Throw on copy only
- AllocVec vec(forward_iterator<bool*>(a), forward_iterator<bool*>(a + 2), alloc);
- } catch (int) {
- }
- check_new_delete_called();
-
-#if TEST_STD_VER >= 11
- try { // Throw in vector(const vector&, const allocator_type&) from allocator
- throwing_allocator<bool> alloc(false, false);
- AllocVec vec(alloc);
- vec.push_back(true);
- alloc.throw_on_copy_ = true;
- AllocVec vec2(vec, alloc);
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(vector&&, const allocator_type&) from allocator
- throwing_allocator<bool> alloc(false, false);
- AllocVec vec(alloc);
- vec.push_back(true);
- alloc.throw_on_copy_ = true;
- AllocVec vec2(std::move(vec), alloc);
- } catch (int) {
- }
- check_new_delete_called();
-
- try { // Throw in vector(initializer_list<value_type>, const allocator_type&) constructor from allocator
- throwing_allocator<bool> alloc(false, true); // Throw on copy only
- AllocVec vec({true, true}, alloc);
- } catch (int) {
- }
- check_new_delete_called();
-#endif // TEST_STD_VER >= 11
-
- return 0;
-}
diff --git a/libcxx/test/support/exception_test_helpers.h b/libcxx/test/std/containers/sequences/vector/common.h
similarity index 87%
rename from libcxx/test/support/exception_test_helpers.h
rename to libcxx/test/std/containers/sequences/vector/common.h
index 00a9e54331d0ab..31f223007b187a 100644
--- a/libcxx/test/support/exception_test_helpers.h
+++ b/libcxx/test/std/containers/sequences/vector/common.h
@@ -6,8 +6,13 @@
//
//===----------------------------------------------------------------------===//
-#ifndef EXCEPTION_TEST_HELPERS_H
-#define EXCEPTION_TEST_HELPERS_H
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <cassert>
+#include <cstddef>
+#include <memory>
+#include <type_traits>
#include "count_new.h"
@@ -18,7 +23,9 @@ struct throwing_allocator {
bool throw_on_copy_ = false;
- throwing_allocator(bool throw_on_ctor = true, bool throw_on_copy = false) : throw_on_copy_(throw_on_copy) {
+ throwing_allocator() : throwing_allocator(/*throw_on_ctor = */ true, /*throw_on_copy = */ false) {}
+
+ explicit throwing_allocator(bool throw_on_ctor, bool throw_on_copy) : throw_on_copy_(throw_on_copy) {
if (throw_on_ctor)
throw 0;
}
@@ -84,4 +91,4 @@ inline void check_new_delete_called() {
assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
}
-#endif // EXCEPTION_TEST_HELPERS_H
\ No newline at end of file
+#endif // COMMON_H
More information about the libcxx-commits
mailing list