blob: 5dc9f710ab74175a2b95093bdec904c53c33c2a7 [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
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());
Eric Fiselier7b86ce52015-07-18 21:17:16 +000042 ((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;
54 assert(ap);
55 assert(A_constructed);
56 delete ap;
57 assert(!A_constructed);
58}