| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: no-exceptions |
| 10 | // UNSUPPORTED: sanitizer-new-delete |
| 11 | |
| 12 | // GCC warns about allocating numeric_limits<size_t>::max() being too large (which we test here) |
| 13 | // ADDITIONAL_COMPILE_FLAGS(gcc): -Wno-alloc-size-larger-than |
| 14 | |
| 15 | // Libc++ when built for z/OS doesn't contain the aligned allocation functions, |
| 16 | // nor does the dynamic library shipped with z/OS. |
| 17 | // XFAIL: target={{.+}}-zos{{.*}} |
| 18 | |
| 19 | #include <new> |
| 20 | #include <cassert> |
| 21 | #include <limits> |
| 22 | #include <cstdlib> |
| 23 | |
| 24 | struct construction_key {}; |
| 25 | struct my_bad_alloc : std::bad_alloc { |
| 26 | my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); } |
| 27 | my_bad_alloc(construction_key) : self(this) {} |
| 28 | const my_bad_alloc* const self; |
| 29 | }; |
| 30 | |
| 31 | int new_handler_called = 0; |
| 32 | |
| 33 | void my_new_handler() { |
| 34 | ++new_handler_called; |
| 35 | throw my_bad_alloc(construction_key()); |
| 36 | } |
| 37 | |
| 38 | int main(int, char**) { |
| 39 | std::set_new_handler(my_new_handler); |
| 40 | try { |
| 41 | void* x = operator new(std::numeric_limits<std::size_t>::max(), static_cast<std::align_val_t>(32)); |
| 42 | (void)x; |
| 43 | assert(false); |
| 44 | } catch (my_bad_alloc const& e) { |
| 45 | assert(new_handler_called == 1); |
| 46 | assert(e.self == &e); |
| 47 | } catch (...) { |
| 48 | assert(false); |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |