blob: b0db4a8e57e2a936bfa1d903749523915a73d4e1 [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 [] (nothrow)
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 {
Howard Hinnant5ce391e2013-01-16 17:56:06 +000041 void*volatile vp = operator new [] (std::numeric_limits<std::size_t>::max(), std::nothrow);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 assert(new_handler_called == 1);
43 assert(vp == 0);
44 }
45 catch (...)
46 {
47 assert(false);
48 }
49 A* ap = new(std::nothrow) A[3];
50 assert(ap);
51 assert(A_constructed == 3);
52 delete [] ap;
53 assert(A_constructed == 0);
54}