From 87da2e6e1471616805c6dabaaa6e8e58890384ca Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 6 Apr 2018 15:11:03 +0200 Subject: [PATCH] tests: prevent crash in glib caused by abort on recursive logging The fail() function from libcheck is doing a longjump() from inside the logging subsystem, preventing the depth counter to be reinitialised to 0. (Seen with g_private_get(&g_log_depth) in a gdb session). --- tests/tests.c | 32 +++++++++++++++++++++++++++----- tests/tests.h | 7 +++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/tests/tests.c b/tests/tests.c index 54918c1a..d0e4c8a3 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -57,17 +58,33 @@ SuiteFunction suites[] = { NULL }; + void mute_logger(G_GNUC_UNUSED const gchar *domain, G_GNUC_UNUSED GLogLevelFlags log_level, G_GNUC_UNUSED const gchar *message, G_GNUC_UNUSED gpointer user_data) { } +int dontfork = 0; +int log_failed = 0; + +void set_mute_logger() { + g_log_set_default_handler(mute_logger, NULL); + if (log_failed) { + log_failed = 0; + fail("There were logs, there should not"); + } +} + void -fail_logger(const gchar *log_domain, GLogLevelFlags log_level, - const gchar *message, G_GNUC_UNUSED gpointer user_data) +fail_logger(const gchar *log_domain G_GNUC_UNUSED, GLogLevelFlags log_level, + const gchar *message G_GNUC_UNUSED, G_GNUC_UNUSED gpointer user_data) { - const char *level_name = NULL; + const char *level_name G_GNUC_UNUSED = NULL; + + if (strncmp(message, "libxmlsec", 9) == 0) { + return; + } switch (log_level) { case G_LOG_LEVEL_ERROR: @@ -91,8 +108,12 @@ fail_logger(const gchar *log_domain, GLogLevelFlags log_level, default: g_assert_not_reached(); } - fail("No logging output expected: message «%s» was emitted for domain «%s» at the level" - " «%s»", message, log_domain, level_name); + if (! dontfork) { + fail("No logging output expected: message «%s» was emitted for domain «%s» at the level «%s»", message, log_domain, level_name); + } + printf("No logging output expected: message «%s» was emitted for domain «%s» at the level «%s»", message, log_domain, level_name); + log_failed = 1; + G_BREAKPOINT(); } static xmlFreeFunc free_func; @@ -156,6 +177,7 @@ main(int argc, char *argv[]) if (dont_fork) { srunner_set_fork_status(sr, CK_NOFORK); } + dontfork = srunner_fork_status(sr) == CK_NOFORK; #ifdef CHECK_IS_XML srunner_set_xml(sr, "result.xml"); #endif diff --git a/tests/tests.h b/tests/tests.h index 2391bd29..bb4bb839 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -73,14 +73,13 @@ fail_unless(g_strcmp0(__tmp, to) != 0, "%s:%i: " #what " is equal to %s", __func__, __LINE__, to); \ } -void mute_logger(G_GNUC_UNUSED const gchar *domain, - G_GNUC_UNUSED GLogLevelFlags log_level, G_GNUC_UNUSED const gchar *message, - G_GNUC_UNUSED gpointer user_data); + +void set_mute_logger(); void fail_logger(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, G_GNUC_UNUSED gpointer user_data); -#define block_lasso_logs g_log_set_default_handler(mute_logger, NULL); +#define block_lasso_logs set_mute_logger(); #define unblock_lasso_logs g_log_set_default_handler(fail_logger, NULL);