// Testcase for overflow handling in operator new[]. // { dg-do run } #include #include struct without_new { char bar[256]; }; struct with_new { char bar[256]; void *operator new[] (size_t sz) { if (sz != -1) abort (); throw std::bad_alloc(); } }; template inline void test (size_t s) { try { new T[s]; abort (); } catch (std::bad_alloc &) { } } template void test_noopt (size_t s) __attribute__((noinline)); template void test_noopt (size_t s) { __asm__ (""); test (s); } template void all_tests () { test(-1); test(size_t(-1) / sizeof (T) + 1); test(size_t(-1) / sizeof (T) + 2); test_noopt(-1); test_noopt(size_t(-1) / sizeof (T) + 1); test_noopt(size_t(-1) / sizeof (T) + 2); } int main () { try { ::operator new(size_t(-1)); abort (); } catch (std::bad_alloc &) { } all_tests (); all_tests (); return 0; }