[glib/2753-vasprintf-loop] glib/tests: Add test to check that we abort on low-memory
- From: Marco Trevisan <marcotrevi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/2753-vasprintf-loop] glib/tests: Add test to check that we abort on low-memory
- Date: Thu, 20 Oct 2022 13:06:38 +0000 (UTC)
commit 9715b484a2a2ca99e28e0d5bba3b85200f8b72a7
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date: Thu Oct 20 15:05:55 2022 +0200
glib/tests: Add test to check that we abort on low-memory
But we should not crash
glib/tests/meson.build | 1 +
glib/tests/messages-low-memory.c | 113 +++++++++++++++++++++++++++++++++++++++
2 files changed, 114 insertions(+)
---
diff --git a/glib/tests/meson.build b/glib/tests/meson.build
index e8f637445b..db906040e8 100644
--- a/glib/tests/meson.build
+++ b/glib/tests/meson.build
@@ -61,6 +61,7 @@ glib_tests = {
'link_args' : cc.get_id() == 'gcc' and cc.version().version_compare('> 6')
? ['-Wno-alloc-size-larger-than'] : [],
},
+ 'messages-low-memory' : {},
'mutex' : {},
'node' : {},
'once' : {},
diff --git a/glib/tests/messages-low-memory.c b/glib/tests/messages-low-memory.c
new file mode 100644
index 0000000000..6f2437045b
--- /dev/null
+++ b/glib/tests/messages-low-memory.c
@@ -0,0 +1,113 @@
+/* Unit tests for gmessages on low-memory
+ *
+ * Copyright (C) 2022 Marco Trevisan
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marco Trevisan <marco trevisan canonical com>
+ */
+
+#include "config.h"
+
+#include <dlfcn.h>
+#include <glib.h>
+#include <signal.h>
+
+static gboolean malloc_eom = FALSE;
+static int expected_signal = 0;
+
+#ifdef ENOMEM
+void *malloc(size_t size)
+{
+ static void *(*real_malloc)(size_t);
+ if (!real_malloc)
+ real_malloc = dlsym (RTLD_NEXT, "malloc");
+
+ if (malloc_eom) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ return real_malloc (size);
+}
+#endif
+
+static gboolean
+check_can_run (void)
+{
+#ifndef ENOMEM
+ g_test_skip ("ENOMEM Not defined");
+ return FALSE;
+#endif
+ return TRUE;
+}
+
+static void
+test_malloc_failures (void)
+{
+ char *str;
+
+ if (!check_can_run ())
+ return;
+
+ errno = 0;
+ malloc_eom = TRUE;
+ g_assert_null (malloc (sizeof (char) * 10));
+ g_assert_cmpint (errno, ==, ENOMEM);
+
+ errno = 0;
+ malloc_eom = FALSE;
+ str = malloc (sizeof (char) * 10);
+ g_assert_nonnull (str);
+ g_assert_cmpint (errno, ==, 0);
+
+ g_free (str);
+}
+
+static void
+signal_handler (int signo)
+{
+ malloc_eom = FALSE;
+ g_message ("Got signal %d", signo);
+ g_assert_cmpint (expected_signal, ==, signo);
+ exit (0);
+}
+
+G_NORETURN static void
+test_messages_on_low_memory (void)
+{
+ signal (SIGABRT, &signal_handler);
+ signal (SIGSEGV, &signal_handler);
+
+ malloc_eom = TRUE;
+ expected_signal = SIGABRT;
+ g_message ("Memory is over, but we'll write anyways: %u", g_random_int ());
+
+ g_assert_not_reached ();
+}
+
+int
+main (int argc,
+ char *argv[])
+{
+ g_setenv ("LC_ALL", "C", TRUE);
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/overrides/malloc-failures", test_malloc_failures);
+ g_test_add_func ("/messages/on-low-memory", test_messages_on_low_memory);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]