view gcc/testsuite/g++.dg/init/new39.C @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 04ced10e8804
children
line wrap: on
line source

// Testcase for overflow handling in operator new[].
// { dg-do run }

#include <stdlib.h>
#include <stdexcept>

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 <typename T>
inline void
test (size_t s)
{
  try {
    new T[s];
    abort ();
  } catch (std::bad_alloc &) {
  }
}

template <typename T>
void
test_noopt (size_t s) __attribute__((noinline));

template <typename T>
void
test_noopt (size_t s)
{
  __asm__ ("");
  test<T> (s);
}

template <typename T>
void
all_tests ()
{
  test<T>(-1);
  test<T>(size_t(-1) / sizeof (T) + 1);
  test<T>(size_t(-1) / sizeof (T) + 2);
  test_noopt<T>(-1);
  test_noopt<T>(size_t(-1) / sizeof (T) + 1);
  test_noopt<T>(size_t(-1) / sizeof (T) + 2);
}

int
main ()
{
  try {
    (void) ::operator new(size_t(-1));
    abort ();
  } catch (std::bad_alloc &) {
  }
  all_tests<without_new> ();
  all_tests<with_new> ();
  return 0;
}