Home | History | Annotate | Download | only in new.delete.single
      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 int new_handler_called = 0;
     21 
     22 void new_handler()
     23 {
     24     ++new_handler_called;
     25     std::set_new_handler(0);
     26 }
     27 
     28 bool A_constructed = false;
     29 
     30 struct A
     31 {
     32     A() {A_constructed = true;}
     33     ~A() {A_constructed = false;}
     34 };
     35 
     36 int main()
     37 {
     38     std::set_new_handler(new_handler);
     39     try
     40     {
     41         void* vp = operator new (std::numeric_limits<std::size_t>::max());
     42         ((void)vp);
     43         assert(false);
     44     }
     45     catch (std::bad_alloc&)
     46     {
     47         assert(new_handler_called == 1);
     48     }
     49     catch (...)
     50     {
     51         assert(false);
     52     }
     53     A* ap = new A;
     54     assert(ap);
     55     assert(A_constructed);
     56     delete ap;
     57     assert(!A_constructed);
     58 }
     59