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// GCC warns about allocating numeric_limits<size_t>::max() being too large (which we test here)
13// ADDITIONAL_COMPILE_FLAGS(gcc): -Wno-alloc-size-larger-than
14
15#include <new>
16#include <cassert>
17#include <limits>
18#include <cstdlib>
19
20struct construction_key {};
21struct my_bad_alloc : std::bad_alloc {
22 my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
23 my_bad_alloc(construction_key) : self(this) {}
24 const my_bad_alloc* const self;
25};
26
27int new_handler_called = 0;
28
29void my_new_handler() {
30 ++new_handler_called;
31 throw my_bad_alloc(construction_key());
32}
33
34int main(int, char**) {
35 std::set_new_handler(my_new_handler);
36 try {
37 void* x = operator new(std::numeric_limits<std::size_t>::max());
38 (void)x;
39 assert(false);
40 } catch (my_bad_alloc const& e) {
41 assert(new_handler_called == 1);
42 assert(e.self == &e);
43 } catch (...) {
44 assert(false);
45 }
46 return 0;
47}
48

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