new_array.pass.cpp 1.2 KB

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