various GLib tests: plug memory leaks
[platform/upstream/glib.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 /* We test for errors in optimize-only definitions in gmem.h */
23
24 #pragma GCC optimize (1)
25
26 #include "glib.h"
27 #include <stdlib.h>
28
29 static void
30 mem_overflow (void)
31 {
32   gsize a = G_MAXSIZE / 10 + 10;
33   gsize b = 10;
34   gpointer p, q;
35   typedef char X[10];
36
37   /* "FAIL" here apparently means "fail to overflow"... */
38 #define CHECK_PASS(P)   p = (P); g_assert (p == NULL);
39 #define CHECK_FAIL(P)   p = (P); g_assert (p != NULL);
40
41   CHECK_PASS (g_try_malloc_n (a, a));
42   CHECK_PASS (g_try_malloc_n (a, b));
43   CHECK_PASS (g_try_malloc_n (b, a));
44   CHECK_FAIL (g_try_malloc_n (b, b));
45   g_free (p);
46
47   CHECK_PASS (g_try_malloc0_n (a, a));
48   CHECK_PASS (g_try_malloc0_n (a, b));
49   CHECK_PASS (g_try_malloc0_n (b, a));
50   CHECK_FAIL (g_try_malloc0_n (b, b));
51   g_free (p);
52
53   q = g_malloc (1);
54   CHECK_PASS (g_try_realloc_n (q, a, a));
55   CHECK_PASS (g_try_realloc_n (q, a, b));
56   CHECK_PASS (g_try_realloc_n (q, b, a));
57   CHECK_FAIL (g_try_realloc_n (q, b, b));
58   g_free (p);
59
60   CHECK_PASS (g_try_new (X, a));
61   CHECK_FAIL (g_try_new (X, b));
62   g_free (p);
63
64   CHECK_PASS (g_try_new0 (X, a));
65   CHECK_FAIL (g_try_new0 (X, b));
66   g_free (p);
67
68   q = g_try_malloc (1);
69   CHECK_PASS (g_try_renew (X, q, a));
70   CHECK_FAIL (g_try_renew (X, q, b));
71   free (p);
72
73 #undef CHECK_FAIL
74 #undef CHECK_PASS
75
76 #define CHECK_FAIL(P)   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { p = (P); exit (0); } g_test_trap_assert_failed();
77
78 #define CHECK_PASS(P)   do { \
79       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) \
80         { \
81           p = (P); \
82           g_free (p); \
83           exit (0); \
84         } \
85       \
86       g_test_trap_assert_passed(); \
87     } while (0)
88
89   CHECK_FAIL (g_malloc_n (a, a));
90   CHECK_FAIL (g_malloc_n (a, b));
91   CHECK_FAIL (g_malloc_n (b, a));
92   CHECK_PASS (g_malloc_n (b, b));
93
94   CHECK_FAIL (g_malloc0_n (a, a));
95   CHECK_FAIL (g_malloc0_n (a, b));
96   CHECK_FAIL (g_malloc0_n (b, a));
97   CHECK_PASS (g_malloc0_n (b, b));
98
99   q = g_malloc (1);
100   CHECK_FAIL (g_realloc_n (q, a, a));
101   CHECK_FAIL (g_realloc_n (q, a, b));
102   CHECK_FAIL (g_realloc_n (q, b, a));
103   CHECK_PASS (g_realloc_n (q, b, b));
104   g_free (q);
105
106   CHECK_FAIL (g_new (X, a));
107   CHECK_PASS (g_new (X, b));
108
109   CHECK_FAIL (g_new0 (X, a));
110   CHECK_PASS (g_new0 (X, b));
111
112   q = g_malloc (1);
113   CHECK_FAIL (g_renew (X, q, a));
114   CHECK_PASS (g_renew (X, q, b));
115   g_free (q);
116 }
117
118 typedef struct
119 {
120 } Empty;
121
122 static void
123 empty_alloc (void)
124 {
125   g_test_bug ("615379");
126
127   g_assert_cmpint (sizeof (Empty), ==, 0);
128
129   if (g_test_trap_fork (0, 0))
130     {
131       Empty *empty;
132
133       empty = g_new0 (Empty, 1);
134       g_assert (empty == NULL);
135       exit (0);
136     }
137   g_test_trap_assert_passed ();
138 }
139
140 int
141 main (int   argc,
142       char *argv[])
143 {
144   g_test_init (&argc, &argv, NULL);
145
146   g_test_bug_base ("http://bugzilla.gnome.org/");
147
148   g_test_add_func ("/mem/overflow", mem_overflow);
149   g_test_add_func ("/mem/empty-alloc", empty_alloc);
150
151   return g_test_run();
152 }