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).
This commit is contained in:
Benjamin Dauvergne 2018-04-06 15:11:03 +02:00
parent 48fb1c37ec
commit 87da2e6e14
2 changed files with 30 additions and 9 deletions

View File

@ -23,6 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <config.h>
#include <stdio.h>
#include <check.h>
#include <glib.h>
@ -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

View File

@ -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);