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// UNSUPPORTED: no-exceptions
10// UNSUPPORTED: sanitizer-new-delete
11
12#include <new>
13#include <cassert>
14#include <limits>
15#include <cstdlib>
16
17struct construction_key {};
18struct my_bad_alloc : std::bad_alloc {
19 my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
20 my_bad_alloc(construction_key) : self(this) {}
21 const my_bad_alloc* const self;
22};
23
24int new_handler_called = 0;
25
26void my_new_handler() {
27 ++new_handler_called;
28 throw my_bad_alloc(construction_key());
29}
30
31int main(int, char**) {
32 std::set_new_handler(my_new_handler);
33 try {
34 void* x = operator new[](std::numeric_limits<std::size_t>::max());
35 (void)x;
36 assert(false);
37 } catch (my_bad_alloc const& e) {
38 assert(new_handler_called == 1);
39 assert(e.self == &e);
40 } catch (...) {
41 assert(false);
42 }
43 return 0;
44}
45

source code of libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size.except.pass.cpp