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 
17 struct construction_key {};
18 struct my_bad_alloc : std::bad_alloc {
my_bad_allocmy_bad_alloc19   my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_allocmy_bad_alloc20   my_bad_alloc(construction_key) : self(this) {}
21   const my_bad_alloc* const self;
22 };
23 
24 int new_handler_called = 0;
25 
my_new_handler()26 void my_new_handler() {
27   ++new_handler_called;
28   throw my_bad_alloc(construction_key());
29 }
30 
main(int,char **)31 int 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