gstring: Add critical warning to g_string_append_vprintf()

It was previously possible for this to silently fail, which isn’t great
for program correctness. Instead, raise a critical warning so the
programmer knows to either validate their Unicode inputs, or fix their
format placeholders.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3187
This commit is contained in:
Philip Withnall 2023-11-27 11:18:03 +00:00
parent c56bc6d8d9
commit 707ae4bd79
2 changed files with 10 additions and 0 deletions

View File

@ -1221,6 +1221,10 @@ g_string_append_vprintf (GString *string,
string->len += len; string->len += len;
g_free (buf); g_free (buf);
} }
else
{
g_critical ("Failed to append to string: invalid format/args passed to g_vasprintf()");
}
} }
/** /**

View File

@ -319,11 +319,17 @@ test_string_append_vprintf (void)
string_append_vprintf_va (string, "some %s placeholders", "format"); string_append_vprintf_va (string, "some %s placeholders", "format");
if (g_test_undefined ())
{
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"Failed to append to string*");
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat" #pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args" #pragma GCC diagnostic ignored "-Wformat-extra-args"
string_append_vprintf_va (string, "%l", "invalid"); string_append_vprintf_va (string, "%l", "invalid");
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
g_test_assert_expected_messages ();
}
g_assert_cmpstr (string->str, ==, "firsthalfsome format placeholders"); g_assert_cmpstr (string->str, ==, "firsthalfsome format placeholders");