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 // XFAIL: LIBCXX-WINDOWS-FIXME 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 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 { 34 A() {A_constructed = true;} 35 ~A() {A_constructed = false;} 36 }; 37 38 int main(int, char**) 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 return 0; 64 } 65