• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // test operator new
11 
12 // asan and msan will not call the new handler.
13 // UNSUPPORTED: sanitizer-new-delete
14 
15 #include <new>
16 #include <cstddef>
17 #include <cassert>
18 #include <limits>
19 
20 #include "test_macros.h"
21 
22 int new_handler_called = 0;
23 
my_new_handler()24 void my_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 {
AA34     A() {A_constructed = true;}
~AA35     ~A() {A_constructed = false;}
36 };
37 
main()38 int main()
39 {
40 #ifndef TEST_HAS_NO_EXCEPTIONS
41     std::set_new_handler(my_new_handler);
42     try
43     {
44         void* vp = operator new (std::numeric_limits<std::size_t>::max());
45         ((void)vp);
46         assert(false);
47     }
48     catch (std::bad_alloc&)
49     {
50         assert(new_handler_called == 1);
51     }
52     catch (...)
53     {
54         assert(false);
55     }
56 #endif
57     A* ap = new A;
58     assert(ap);
59     assert(A_constructed);
60     delete ap;
61     assert(!A_constructed);
62 }
63