init.c (build_new_1): Do not check for arithmetic overflow if inner array size is 1.
[official-gcc.git] / gcc / testsuite / g++.dg / init / new40.C
blob4b283a4454d53cc2b36986259b8b21fbd317ce19
1 // Testcase for overflow handling in operator new[].
2 // Optimization of unnecessary overflow checks.
3 // { dg-do run }
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <stdexcept>
9 static size_t magic_allocation_size
10   = 1 + (size_t (1) << (sizeof (size_t) * 8 - 1));
12 struct exc : std::bad_alloc {
15 static size_t expected_size;
17 struct pod_with_new {
18   char ch;
19   void *operator new[] (size_t sz)
20   {
21     if (sz != expected_size)
22       abort ();
23     throw exc ();
24   }
27 struct with_new {
28   char ch;
29   with_new () { }
30   ~with_new () { }
31   void *operator new[] (size_t sz)
32   {
33     if (sz != size_t (-1))
34       abort ();
35     throw exc ();
36   }
39 struct non_pod {
40   char ch;
41   non_pod () { }
42   ~non_pod () { }
45 void *
46 operator new (size_t sz) _GLIBCXX_THROW (std::bad_alloc)
48   if (sz != expected_size)
49     abort ();
50   throw exc ();
53 int
54 main ()
56   if (sizeof (pod_with_new) == 1)
57     expected_size = magic_allocation_size;
58   else
59     expected_size = -1;
61   try {
62     new pod_with_new[magic_allocation_size];
63     abort ();
64   } catch (exc &) {
65   }
67   if (sizeof (with_new) == 1)
68     expected_size = magic_allocation_size;
69   else
70     expected_size = -1;
72   try {
73     new with_new[magic_allocation_size];
74     abort ();
75   } catch (exc &) {
76   }
78   expected_size = magic_allocation_size;
79   try {
80     new char[magic_allocation_size];
81     abort ();
82   } catch (exc &) {
83   }
85   expected_size = -1;
87   try {
88     new pod_with_new[magic_allocation_size][2];
89     abort ();
90   } catch (exc &) {
91   }
93   try {
94     new with_new[magic_allocation_size][2];
95     abort ();
96   } catch (exc &) {
97   }
99   try {
100     new char[magic_allocation_size][2];
101     abort ();
102   } catch (exc &) {
103   }
105   try {
106     new non_pod[magic_allocation_size];
107     abort ();
108   } catch (exc &) {
109   }
111   return 0;