1*eb8650a7SLouis Dionne //===----------------------------------------------------------------------===//
2e434b34fSJonathan Roelofs //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e434b34fSJonathan Roelofs //
7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
8e434b34fSJonathan Roelofs 
98c61114cSLouis Dionne // UNSUPPORTED: no-exceptions
1057e446daSAsiri Rathnayake 
1160ba1fefSLouis Dionne // ___cxa_throw_bad_array_new_length is re-exported from libc++ only starting
1260ba1fefSLouis Dionne // in macosx 10.15
13c360553cSLouis Dionne // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14}}
1460ba1fefSLouis Dionne 
15e434b34fSJonathan Roelofs #include <cxxabi.h>
16cc69d211SLouis Dionne #include <new>
17e434b34fSJonathan Roelofs 
18e434b34fSJonathan Roelofs //  If the expression passed to operator new[] would result in an overflow, the
19e434b34fSJonathan Roelofs //  allocation function is not called, and a std::bad_array_new_length exception
20e434b34fSJonathan Roelofs //  is thrown instead (5.3.4p7).
bad_array_new_length_test()21e434b34fSJonathan Roelofs bool bad_array_new_length_test() {
22e434b34fSJonathan Roelofs     try {
23e434b34fSJonathan Roelofs       // We test this directly because Clang does not currently codegen the
24e434b34fSJonathan Roelofs       // correct call to __cxa_bad_array_new_length, so this test would result
25e434b34fSJonathan Roelofs       // in passing -1 to ::operator new[], which would then throw a
26e434b34fSJonathan Roelofs       // std::bad_alloc, causing the test to fail.
27e434b34fSJonathan Roelofs       __cxxabiv1::__cxa_throw_bad_array_new_length();
28e434b34fSJonathan Roelofs     } catch ( const std::bad_array_new_length &banl ) {
29e434b34fSJonathan Roelofs       return true;
30e434b34fSJonathan Roelofs     }
31e434b34fSJonathan Roelofs     return false;
32e434b34fSJonathan Roelofs }
33e434b34fSJonathan Roelofs 
main(int,char **)34504bc07dSLouis Dionne int main(int, char**) {
35e434b34fSJonathan Roelofs     int ret_val = 0;
36e434b34fSJonathan Roelofs 
37e434b34fSJonathan Roelofs     if ( !bad_array_new_length_test ()) {
38e434b34fSJonathan Roelofs         ret_val = 1;
39e434b34fSJonathan Roelofs     }
40e434b34fSJonathan Roelofs 
41e434b34fSJonathan Roelofs     return ret_val;
42e434b34fSJonathan Roelofs }
43