blob: a1a303529260d1d0e91fd675c838386750ea716f [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
Howard Hinnantcf6dcc32010-08-22 00:31:12 +00009
Eric Fiselier72aab5f2014-11-04 05:11:41 +000010// test operator new[]
Eric Fiselier71915c12014-11-14 02:55:16 +000011// NOTE: asan and msan will not call the new handler.
Eric Fiselier07a4bec2015-03-10 20:46:04 +000012// UNSUPPORTED: sanitizer-new-delete
Eric Fiselier72aab5f2014-11-04 05:11:41 +000013
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000014
15#include <new>
16#include <cstddef>
17#include <cassert>
18#include <limits>
19
20int new_handler_called = 0;
21
22void new_handler()
23{
24 ++new_handler_called;
25 std::set_new_handler(0);
26}
27
28int A_constructed = 0;
29
30struct A
31{
32 A() {++A_constructed;}
33 ~A() {--A_constructed;}
34};
35
36int main()
37{
38 std::set_new_handler(new_handler);
39 try
40 {
Eric Fiselier7b86ce52015-07-18 21:17:16 +000041 void* volatile vp = operator new[] (std::numeric_limits<std::size_t>::max());
42 ((void)vp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043 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[3];
54 assert(ap);
55 assert(A_constructed == 3);
56 delete [] ap;
57 assert(A_constructed == 0);
58}