collate: Don't segfault on bad input

This commit is contained in:
Matthias Clasen 2023-11-26 22:41:26 +00:00 committed by Philip Withnall
parent e67a36c479
commit 81ec27c33b
2 changed files with 16 additions and 0 deletions

View File

@ -401,6 +401,8 @@ g_utf8_collate_key (const gchar *str,
str_norm = _g_utf8_normalize_wc (str, len, G_NORMALIZE_ALL_COMPOSE);
g_return_val_if_fail (str_norm != NULL, NULL);
xfrm_len = wcsxfrm (NULL, (wchar_t *)str_norm, 0);
result_wc = g_new (wchar_t, xfrm_len + 1);
wcsxfrm (result_wc, (wchar_t *)str_norm, xfrm_len + 1);

View File

@ -182,6 +182,19 @@ test_unicode_normalize_invalid (void)
}
}
static void
test_unicode_normalize_bad_length (void)
{
const char *input = "fórmula, vol. 2 (deluxe edition)";
gsize len = 2;
char *output;
output = g_utf8_normalize (input, len, G_NORMALIZE_ALL_COMPOSE);
g_assert_null (output);
g_free (output);
}
int
main (int argc, char **argv)
{
@ -190,6 +203,7 @@ main (int argc, char **argv)
g_test_add_func ("/unicode/normalize", test_unicode_normalize);
g_test_add_func ("/unicode/normalize-invalid",
test_unicode_normalize_invalid);
g_test_add_func ("/unicode/normalize/bad-length", test_unicode_normalize_bad_length);
return g_test_run ();
}