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