//===----------------------------------------------------------------------===// // // 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 // (bug report: https://llvm.org/PR58392) // Check that vector constructors don't leak memory when an operation inside the constructor throws an exception #include #include #include #include #include "../vector/common.h" #include "count_new.h" #include "test_iterators.h" int main(int, char**) { using AllocVec = std::vector >; try { // Throw in vector() from allocator AllocVec vec; } catch (int) { } check_new_delete_called(); #if TEST_STD_VER >= 14 try { // Throw in vector(size_type, const allocator_type&) from allocator throwing_allocator 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 try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator throwing_allocator 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 vec( throwing_iterator(), throwing_iterator(2)); } catch (int) { } check_new_delete_called(); try { // Throw in vector(InputIterator, InputIterator) from forward iterator std::vector vec( throwing_iterator(), throwing_iterator(2)); } catch (int) { } check_new_delete_called(); try { // Throw in vector(InputIterator, InputIterator) from allocator bool a[] = {true, true}; AllocVec vec(cpp17_input_iterator(a), cpp17_input_iterator(a + 2)); } catch (int) { } check_new_delete_called(); try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator std::allocator alloc; std::vector vec( throwing_iterator(), throwing_iterator(2), alloc); } catch (int) { } check_new_delete_called(); try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator std::allocator alloc; std::vector vec(throwing_iterator(), throwing_iterator(2), alloc); } catch (int) { } check_new_delete_called(); try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator bool a[] = {true, false}; throwing_allocator alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true); AllocVec vec(cpp17_input_iterator(a), cpp17_input_iterator(a + 2), alloc); } catch (int) { } check_new_delete_called(); try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator bool a[] = {true, false}; throwing_allocator alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true); AllocVec vec(forward_iterator(a), forward_iterator(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 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 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, const allocator_type&) constructor from allocator throwing_allocator 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; }