aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.8/gcc/testsuite/g++.dg/init/new39.C
blob: f274ebb7b9d69e599225a77269b3cdf0593fa136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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 {
    ::operator new(size_t(-1));
    abort ();
  } catch (std::bad_alloc &) {
  }
  all_tests<without_new> ();
  all_tests<with_new> ();
  return 0;
}