Initial commit
[platform/upstream/glib2.0.git] / glib / tests / mem-overflow.c
1 /* Unit tests for g
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
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.
7  *
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.
11  *
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.
20  */
21
22 #include "glib.h"
23 #include <stdlib.h>
24
25 static void
26 mem_overflow (void)
27 {
28   gsize a = G_MAXSIZE / 10 + 10;
29   gsize b = 10;
30   gpointer p, q;
31   typedef char X[10];
32
33 #define CHECK_PASS(P)   p = (P); g_assert (p == NULL);
34 #define CHECK_FAIL(P)   p = (P); g_assert (p != NULL);
35
36   CHECK_PASS (g_try_malloc_n (a, a));
37   CHECK_PASS (g_try_malloc_n (a, b));
38   CHECK_PASS (g_try_malloc_n (b, a));
39   CHECK_FAIL (g_try_malloc_n (b, b));
40
41   CHECK_PASS (g_try_malloc0_n (a, a));
42   CHECK_PASS (g_try_malloc0_n (a, b));
43   CHECK_PASS (g_try_malloc0_n (b, a));
44   CHECK_FAIL (g_try_malloc0_n (b, b));
45
46   q = g_malloc (1);
47   CHECK_PASS (g_try_realloc_n (q, a, a));
48   CHECK_PASS (g_try_realloc_n (q, a, b));
49   CHECK_PASS (g_try_realloc_n (q, b, a));
50   CHECK_FAIL (g_try_realloc_n (q, b, b));
51   free (p);
52
53   CHECK_PASS (g_try_new (X, a));
54   CHECK_FAIL (g_try_new (X, b));
55
56   CHECK_PASS (g_try_new0 (X, a));
57   CHECK_FAIL (g_try_new0 (X, b));
58
59   q = g_try_malloc (1);
60   CHECK_PASS (g_try_renew (X, q, a));
61   CHECK_FAIL (g_try_renew (X, q, b));
62   free (p);
63
64 #undef CHECK_FAIL
65 #undef CHECK_PASS
66
67 #define CHECK_FAIL(P)   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { p = (P); exit (0); } g_test_trap_assert_failed();
68 #define CHECK_PASS(P)   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { p = (P); exit (0); } g_test_trap_assert_passed();
69
70   CHECK_FAIL (g_malloc_n (a, a));
71   CHECK_FAIL (g_malloc_n (a, b));
72   CHECK_FAIL (g_malloc_n (b, a));
73   CHECK_PASS (g_malloc_n (b, b));
74
75   CHECK_FAIL (g_malloc0_n (a, a));
76   CHECK_FAIL (g_malloc0_n (a, b));
77   CHECK_FAIL (g_malloc0_n (b, a));
78   CHECK_PASS (g_malloc0_n (b, b));
79
80   q = g_malloc (1);
81   CHECK_FAIL (g_realloc_n (q, a, a));
82   CHECK_FAIL (g_realloc_n (q, a, b));
83   CHECK_FAIL (g_realloc_n (q, b, a));
84   CHECK_PASS (g_realloc_n (q, b, b));
85   free (q);
86
87   CHECK_FAIL (g_new (X, a));
88   CHECK_PASS (g_new (X, b));
89
90   CHECK_FAIL (g_new0 (X, a));
91   CHECK_PASS (g_new0 (X, b));
92
93   q = g_malloc (1);
94   CHECK_FAIL (g_renew (X, q, a));
95   CHECK_PASS (g_renew (X, q, b));
96   free (q);
97 }
98
99 int
100 main (int   argc,
101       char *argv[])
102 {
103   g_test_init (&argc, &argv, NULL);
104
105   g_test_add_func ("/mem/overflow", mem_overflow);
106
107   return g_test_run();
108 }