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 
12 // test operator new
13 
14 // asan and msan will not call the new handler.
15 // UNSUPPORTED: sanitizer-new-delete
16 
17 #include <new>
18 #include <cstddef>
19 #include <cassert>
20 #include <limits>
21 
22 int new_handler_called = 0;
23 
24 void new_handler()
25 {
26     ++new_handler_called;
27     std::set_new_handler(0);
28 }
29 
30 bool A_constructed = false;
31 
32 struct A
33 {
34     A() {A_constructed = true;}
35     ~A() {A_constructed = false;}
36 };
37 
38 int main()
39 {
40     std::set_new_handler(new_handler);
41     try
42     {
43         void* vp = operator new (std::numeric_limits<std::size_t>::max());
44         ((void)vp);
45         assert(false);
46     }
47     catch (std::bad_alloc&)
48     {
49         assert(new_handler_called == 1);
50     }
51     catch (...)
52     {
53         assert(false);
54     }
55     A* ap = new A;
56     assert(ap);
57     assert(A_constructed);
58     delete ap;
59     assert(!A_constructed);
60 }
61