Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Howard Hinnant | cf6dcc3 | 2010-08-22 00:31:12 +0000 | [diff] [blame] | 9 | |
Eric Fiselier | 71915c1 | 2014-11-14 02:55:16 +0000 | [diff] [blame] | 10 | // test operator new |
| 11 | |
| 12 | // asan and msan will not call the new handler. |
Eric Fiselier | 07a4bec | 2015-03-10 20:46:04 +0000 | [diff] [blame] | 13 | // UNSUPPORTED: sanitizer-new-delete |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | |
| 15 | #include <new> |
| 16 | #include <cstddef> |
| 17 | #include <cassert> |
| 18 | #include <limits> |
| 19 | |
| 20 | int new_handler_called = 0; |
| 21 | |
| 22 | void new_handler() |
| 23 | { |
| 24 | ++new_handler_called; |
| 25 | std::set_new_handler(0); |
| 26 | } |
| 27 | |
| 28 | bool A_constructed = false; |
| 29 | |
| 30 | struct A |
| 31 | { |
| 32 | A() {A_constructed = true;} |
| 33 | ~A() {A_constructed = false;} |
| 34 | }; |
| 35 | |
| 36 | int 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 Fiselier | 7b86ce5 | 2015-07-18 21:17:16 +0000 | [diff] [blame] | 42 | ((void)vp); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | 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 | } |