1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // XFAIL: libcpp-no-exceptions
11 // test operator new
12 
13 // asan and msan will not call the new handler.
14 // UNSUPPORTED: sanitizer-new-delete
15 
16 #include <new>
17 #include <cstddef>
18 #include <cassert>
19 #include <limits>
20 
21 int new_handler_called = 0;
22 
23 void new_handler()
24 {
25     ++new_handler_called;
26     std::set_new_handler(0);
27 }
28 
29 bool A_constructed = false;
30 
31 struct A
32 {
33     A() {A_constructed = true;}
34     ~A() {A_constructed = false;}
35 };
36 
37 int main()
38 {
39     std::set_new_handler(new_handler);
40     try
41     {
42         void* vp = operator new (std::numeric_limits<std::size_t>::max());
43         ((void)vp);
44         assert(false);
45     }
46     catch (std::bad_alloc&)
47     {
48         assert(new_handler_called == 1);
49     }
50     catch (...)
51     {
52         assert(false);
53     }
54     A* ap = new A;
55     assert(ap);
56     assert(A_constructed);
57     delete ap;
58     assert(!A_constructed);
59 }
60