blob: 8c095ad32db42afe7335417b19111e196224fb46 [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 Fiselier71915c12014-11-14 02:55:16 +000010// test operator new (nothrow)
11
12// asan and msan will not call the new handler.
Eric Fiselier07a4bec2015-03-10 20:46:04 +000013// UNSUPPORTED: sanitizer-new-delete
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
28bool A_constructed = false;
29
30struct A
31{
32 A() {A_constructed = true;}
33 ~A() {A_constructed = false;}
34};
35
36int main()
37{
38 std::set_new_handler(new_handler);
39 try
40 {
41 void* vp = operator new (std::numeric_limits<std::size_t>::max(), std::nothrow);
42 assert(new_handler_called == 1);
43 assert(vp == 0);
44 }
45 catch (...)
46 {
47 assert(false);
48 }
49 A* ap = new(std::nothrow) A;
50 assert(ap);
51 assert(A_constructed);
52 delete ap;
53 assert(!A_constructed);
54}