Add some more cases to the app-id unit tests
[glib.git] / glib / tests / mem-overflow.c
blobe0c28e5e3e0c17c4150b73ae5acc9d117ea2bd3d
1 /* Unit tests for g
2 * Copyright (C) 2010 Red Hat, Inc.
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 /* We test for errors in optimize-only definitions in gmem.h */
24 #ifdef __GNUC__
25 #pragma GCC optimize (1)
26 #endif
28 #include "glib.h"
29 #include <stdlib.h>
31 static gsize a = G_MAXSIZE / 10 + 10;
32 static gsize b = 10;
33 typedef char X[10];
35 #define MEM_OVERFLOW_TEST(name, code) \
36 static void \
37 mem_overflow_ ## name (void) \
38 { \
39 gpointer p; \
40 code; \
41 g_free (p); \
42 exit (0); \
45 MEM_OVERFLOW_TEST (malloc_n_a_a, p = g_malloc_n (a, a))
46 MEM_OVERFLOW_TEST (malloc_n_a_b, p = g_malloc_n (a, b))
47 MEM_OVERFLOW_TEST (malloc_n_b_a, p = g_malloc_n (b, a))
48 MEM_OVERFLOW_TEST (malloc_n_b_b, p = g_malloc_n (b, b))
50 MEM_OVERFLOW_TEST (malloc0_n_a_a, p = g_malloc0_n (a, a))
51 MEM_OVERFLOW_TEST (malloc0_n_a_b, p = g_malloc0_n (a, b))
52 MEM_OVERFLOW_TEST (malloc0_n_b_a, p = g_malloc0_n (b, a))
53 MEM_OVERFLOW_TEST (malloc0_n_b_b, p = g_malloc0_n (b, b))
55 MEM_OVERFLOW_TEST (realloc_n_a_a, p = g_malloc (1); p = g_realloc_n (p, a, a))
56 MEM_OVERFLOW_TEST (realloc_n_a_b, p = g_malloc (1); p = g_realloc_n (p, a, b))
57 MEM_OVERFLOW_TEST (realloc_n_b_a, p = g_malloc (1); p = g_realloc_n (p, b, a))
58 MEM_OVERFLOW_TEST (realloc_n_b_b, p = g_malloc (1); p = g_realloc_n (p, b, b))
60 MEM_OVERFLOW_TEST (new_a, p = g_new (X, a))
61 MEM_OVERFLOW_TEST (new_b, p = g_new (X, b))
63 MEM_OVERFLOW_TEST (new0_a, p = g_new0 (X, a))
64 MEM_OVERFLOW_TEST (new0_b, p = g_new0 (X, b))
66 MEM_OVERFLOW_TEST (renew_a, p = g_malloc (1); p = g_renew (X, p, a))
67 MEM_OVERFLOW_TEST (renew_b, p = g_malloc (1); p = g_renew (X, p, b))
69 static void
70 mem_overflow_malloc_0 (void)
72 gpointer p;
74 p = g_malloc (0);
75 g_assert (p == NULL);
78 static void
79 mem_overflow_realloc_0 (void)
81 gpointer p;
83 p = g_malloc (10);
84 g_assert (p != NULL);
85 p = g_realloc (p, 0);
86 g_assert (p == NULL);
89 static void
90 mem_overflow (void)
92 gpointer p, q;
94 /* "FAIL" here apparently means "fail to overflow"... */
95 #define CHECK_PASS(P) p = (P); g_assert (p == NULL);
96 #define CHECK_FAIL(P) p = (P); g_assert (p != NULL);
98 CHECK_PASS (g_try_malloc_n (a, a));
99 CHECK_PASS (g_try_malloc_n (a, b));
100 CHECK_PASS (g_try_malloc_n (b, a));
101 CHECK_FAIL (g_try_malloc_n (b, b));
102 g_free (p);
104 CHECK_PASS (g_try_malloc0_n (a, a));
105 CHECK_PASS (g_try_malloc0_n (a, b));
106 CHECK_PASS (g_try_malloc0_n (b, a));
107 CHECK_FAIL (g_try_malloc0_n (b, b));
108 g_free (p);
110 q = g_malloc (1);
111 CHECK_PASS (g_try_realloc_n (q, a, a));
112 CHECK_PASS (g_try_realloc_n (q, a, b));
113 CHECK_PASS (g_try_realloc_n (q, b, a));
114 CHECK_FAIL (g_try_realloc_n (q, b, b));
115 g_free (p);
117 CHECK_PASS (g_try_new (X, a));
118 CHECK_FAIL (g_try_new (X, b));
119 g_free (p);
121 CHECK_PASS (g_try_new0 (X, a));
122 CHECK_FAIL (g_try_new0 (X, b));
123 g_free (p);
125 q = g_try_malloc (1);
126 CHECK_PASS (g_try_renew (X, q, a));
127 CHECK_FAIL (g_try_renew (X, q, b));
128 free (p);
130 #define CHECK_SUBPROCESS_FAIL(name) do { \
131 if (g_test_undefined ()) \
133 g_test_trap_subprocess ("/mem/overflow/subprocess/" #name, 0, 0); \
134 g_test_trap_assert_failed(); \
136 } while (0)
138 #define CHECK_SUBPROCESS_PASS(name) do { \
139 if (g_test_undefined ()) \
141 g_test_trap_subprocess ("/mem/overflow/subprocess/" #name, 0, 0); \
142 g_test_trap_assert_passed(); \
144 } while (0)
146 CHECK_SUBPROCESS_FAIL (malloc_n_a_a);
147 CHECK_SUBPROCESS_FAIL (malloc_n_a_b);
148 CHECK_SUBPROCESS_FAIL (malloc_n_b_a);
149 CHECK_SUBPROCESS_PASS (malloc_n_b_b);
151 CHECK_SUBPROCESS_FAIL (malloc0_n_a_a);
152 CHECK_SUBPROCESS_FAIL (malloc0_n_a_b);
153 CHECK_SUBPROCESS_FAIL (malloc0_n_b_a);
154 CHECK_SUBPROCESS_PASS (malloc0_n_b_b);
156 CHECK_SUBPROCESS_FAIL (realloc_n_a_a);
157 CHECK_SUBPROCESS_FAIL (realloc_n_a_b);
158 CHECK_SUBPROCESS_FAIL (realloc_n_b_a);
159 CHECK_SUBPROCESS_PASS (realloc_n_b_b);
161 CHECK_SUBPROCESS_FAIL (new_a);
162 CHECK_SUBPROCESS_PASS (new_b);
164 CHECK_SUBPROCESS_FAIL (new0_a);
165 CHECK_SUBPROCESS_PASS (new0_b);
167 CHECK_SUBPROCESS_FAIL (renew_a);
168 CHECK_SUBPROCESS_PASS (renew_b);
170 CHECK_SUBPROCESS_PASS (malloc_0);
171 CHECK_SUBPROCESS_PASS (realloc_0);
174 #ifdef __GNUC__
175 typedef struct
177 } Empty;
179 static void
180 empty_alloc_subprocess (void)
182 Empty *empty;
184 empty = g_new0 (Empty, 1);
185 g_assert (empty == NULL);
186 exit (0);
189 static void
190 empty_alloc (void)
192 g_test_bug ("615379");
194 g_assert_cmpint (sizeof (Empty), ==, 0);
196 g_test_trap_subprocess ("/mem/empty-alloc/subprocess", 0, 0);
197 g_test_trap_assert_passed ();
199 #endif
202 main (int argc,
203 char *argv[])
205 g_test_init (&argc, &argv, NULL);
207 g_test_bug_base ("http://bugzilla.gnome.org/");
209 g_test_add_func ("/mem/overflow", mem_overflow);
210 g_test_add_func ("/mem/overflow/subprocess/malloc_n_a_a", mem_overflow_malloc_n_a_a);
211 g_test_add_func ("/mem/overflow/subprocess/malloc_n_a_b", mem_overflow_malloc_n_a_b);
212 g_test_add_func ("/mem/overflow/subprocess/malloc_n_b_a", mem_overflow_malloc_n_b_a);
213 g_test_add_func ("/mem/overflow/subprocess/malloc_n_b_b", mem_overflow_malloc_n_b_b);
214 g_test_add_func ("/mem/overflow/subprocess/malloc0_n_a_a", mem_overflow_malloc0_n_a_a);
215 g_test_add_func ("/mem/overflow/subprocess/malloc0_n_a_b", mem_overflow_malloc0_n_a_b);
216 g_test_add_func ("/mem/overflow/subprocess/malloc0_n_b_a", mem_overflow_malloc0_n_b_a);
217 g_test_add_func ("/mem/overflow/subprocess/malloc0_n_b_b", mem_overflow_malloc0_n_b_b);
218 g_test_add_func ("/mem/overflow/subprocess/realloc_n_a_a", mem_overflow_realloc_n_a_a);
219 g_test_add_func ("/mem/overflow/subprocess/realloc_n_a_b", mem_overflow_realloc_n_a_b);
220 g_test_add_func ("/mem/overflow/subprocess/realloc_n_b_a", mem_overflow_realloc_n_b_a);
221 g_test_add_func ("/mem/overflow/subprocess/realloc_n_b_b", mem_overflow_realloc_n_b_b);
222 g_test_add_func ("/mem/overflow/subprocess/new_a", mem_overflow_new_a);
223 g_test_add_func ("/mem/overflow/subprocess/new_b", mem_overflow_new_b);
224 g_test_add_func ("/mem/overflow/subprocess/new0_a", mem_overflow_new0_a);
225 g_test_add_func ("/mem/overflow/subprocess/new0_b", mem_overflow_new0_b);
226 g_test_add_func ("/mem/overflow/subprocess/renew_a", mem_overflow_renew_a);
227 g_test_add_func ("/mem/overflow/subprocess/renew_b", mem_overflow_renew_b);
228 g_test_add_func ("/mem/overflow/subprocess/malloc_0", mem_overflow_malloc_0);
229 g_test_add_func ("/mem/overflow/subprocess/realloc_0", mem_overflow_realloc_0);
231 #ifdef __GNUC__
232 g_test_add_func ("/mem/empty-alloc", empty_alloc);
233 g_test_add_func ("/mem/empty-alloc/subprocess", empty_alloc_subprocess);
234 #endif
236 return g_test_run();