summaryrefslogtreecommitdiff
path: root/glib/tests/mem-overflow.c
diff options
context:
space:
mode:
authorBehdad Esfahbod <behdad@behdad.org>2010-02-02 23:48:42 -0500
committerBehdad Esfahbod <behdad@behdad.org>2010-03-03 17:54:49 -0500
commit343cbf25c7104f782b9d0070cb623c7605dab646 (patch)
tree670c4c7630705b6aebb13bd16b81fb8c4d6a40e4 /glib/tests/mem-overflow.c
parent373f3d8b52ca8b08de0af6062eb284c2a7a856d9 (diff)
downloadglib-343cbf25c7104f782b9d0070cb623c7605dab646.tar.gz
Bug 608196 - Overflow-safe g_new family
New public API: g_malloc_n g_malloc0_n g_realloc_n g_try_malloc_n g_try_malloc0_n g_try_realloc_n
Diffstat (limited to 'glib/tests/mem-overflow.c')
-rw-r--r--glib/tests/mem-overflow.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/glib/tests/mem-overflow.c b/glib/tests/mem-overflow.c
new file mode 100644
index 000000000..f78c7b734
--- /dev/null
+++ b/glib/tests/mem-overflow.c
@@ -0,0 +1,108 @@
+/* Unit tests for g
+ * Copyright (C) 2010 Red Hat, Inc.
+ *
+ * This work is provided "as is"; redistribution and modification
+ * in whole or in part, in any medium, physical or electronic is
+ * permitted without restriction.
+ *
+ * This work is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * In no event shall the authors or contributors be liable for any
+ * direct, indirect, incidental, special, exemplary, or consequential
+ * damages (including, but not limited to, procurement of substitute
+ * goods or services; loss of use, data, or profits; or business
+ * interruption) however caused and on any theory of liability, whether
+ * in contract, strict liability, or tort (including negligence or
+ * otherwise) arising in any way out of the use of this software, even
+ * if advised of the possibility of such damage.
+ */
+
+#include "glib.h"
+#include <stdlib.h>
+
+static void
+mem_overflow (void)
+{
+ gsize a = G_MAXSIZE / 10 + 10;
+ gsize b = 10;
+ gpointer p, q;
+ typedef char X[10];
+
+#define CHECK_PASS(P) p = (P); g_assert (p == NULL);
+#define CHECK_FAIL(P) p = (P); g_assert (p != NULL);
+
+ CHECK_PASS (g_try_malloc_n (a, a));
+ CHECK_PASS (g_try_malloc_n (a, b));
+ CHECK_PASS (g_try_malloc_n (b, a));
+ CHECK_FAIL (g_try_malloc_n (b, b));
+
+ CHECK_PASS (g_try_malloc0_n (a, a));
+ CHECK_PASS (g_try_malloc0_n (a, b));
+ CHECK_PASS (g_try_malloc0_n (b, a));
+ CHECK_FAIL (g_try_malloc0_n (b, b));
+
+ q = g_malloc (1);
+ CHECK_PASS (g_try_realloc_n (q, a, a));
+ CHECK_PASS (g_try_realloc_n (q, a, b));
+ CHECK_PASS (g_try_realloc_n (q, b, a));
+ CHECK_FAIL (g_try_realloc_n (q, b, b));
+ free (p);
+
+ CHECK_PASS (g_try_new (X, a));
+ CHECK_FAIL (g_try_new (X, b));
+
+ CHECK_PASS (g_try_new0 (X, a));
+ CHECK_FAIL (g_try_new0 (X, b));
+
+ q = g_try_malloc (1);
+ CHECK_PASS (g_try_renew (X, q, a));
+ CHECK_FAIL (g_try_renew (X, q, b));
+ free (p);
+
+#undef CHECK_EQ
+#undef CHECK_NEQ
+
+#define CHECK_FAIL(P) if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { p = (P); exit (0); } g_test_trap_assert_failed();
+#define CHECK_PASS(P) if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { p = (P); exit (0); } g_test_trap_assert_passed();
+
+ CHECK_FAIL (g_malloc_n (a, a));
+ CHECK_FAIL (g_malloc_n (a, b));
+ CHECK_FAIL (g_malloc_n (b, a));
+ CHECK_PASS (g_malloc_n (b, b));
+
+ CHECK_FAIL (g_malloc0_n (a, a));
+ CHECK_FAIL (g_malloc0_n (a, b));
+ CHECK_FAIL (g_malloc0_n (b, a));
+ CHECK_PASS (g_malloc0_n (b, b));
+
+ q = g_malloc (1);
+ CHECK_FAIL (g_realloc_n (q, a, a));
+ CHECK_FAIL (g_realloc_n (q, a, b));
+ CHECK_FAIL (g_realloc_n (q, b, a));
+ CHECK_PASS (g_realloc_n (q, b, b));
+ free (q);
+
+ CHECK_FAIL (g_new (X, a));
+ CHECK_PASS (g_new (X, b));
+
+ CHECK_FAIL (g_new0 (X, a));
+ CHECK_PASS (g_new0 (X, b));
+
+ q = g_malloc (1);
+ CHECK_FAIL (g_renew (X, q, a));
+ CHECK_PASS (g_renew (X, q, b));
+ free (q);
+}
+
+int
+main (int argc,
+ char *argv[])
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/mem/overflow", mem_overflow);
+
+ return g_test_run();
+}