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 // test operator new 10 11 // asan and msan will not call the new handler. 12 // UNSUPPORTED: sanitizer-new-delete 13 14 #include <new> 15 #include <cstddef> 16 #include <cassert> 17 #include <limits> 18 19 #include "test_macros.h" 20 21 int new_handler_called = 0; 22 my_new_handler()23void my_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 { AA33 A() {A_constructed = true;} ~AA34 ~A() {A_constructed = false;} 35 }; 36 main(int,char **)37int main(int, char**) 38 { 39 #ifndef TEST_HAS_NO_EXCEPTIONS 40 std::set_new_handler(my_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 #endif 56 A* ap = new A; 57 assert(ap); 58 assert(A_constructed); 59 delete ap; 60 assert(!A_constructed); 61 62 return 0; 63 } 64