From bd6a22c4ab287bef4247cb3eb0c610f298c9c2ce Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 26 Nov 2023 21:52:36 -0500 Subject: [PATCH] Be more robust against printf failures If an 'output error' is encountered, vsnprintf() will return -1, causing g_printf_string_upper_bound() to return 0. Handle this case in g_vasprintf() without crashing. Fixes: #3187 --- glib/gprintf.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/glib/gprintf.c b/glib/gprintf.c index a0ccef9ab..172418d7e 100644 --- a/glib/gprintf.c +++ b/glib/gprintf.c @@ -358,19 +358,23 @@ g_vasprintf (gchar **string, #else { - va_list args2; - - va_copy (args2, args); - *string = g_new (gchar, g_printf_string_upper_bound (format, args)); - len = _g_vsprintf (*string, format, args2); - va_end (args2); - - if (len < 0) + if (*string == NULL) + len = -1; + else { - g_free (*string); - *string = NULL; + va_list args2; + + va_copy (args2, args); + len = _g_vsprintf (*string, format, args2); + va_end (args2); + + if (len < 0) + { + g_free (*string); + *string = NULL; + } } } #endif