gkeyfile: Move allocation of value until after error checking

This doesn’t affect performance in the normal case of a valid key file,
but does improve performance when handling largely-invalid key files.

oss-fuzz#31796

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2021-03-14 14:09:34 +00:00
parent b07408fe5d
commit cdcc06bbc5

View File

@ -1398,8 +1398,6 @@ g_key_file_parse_key_value_pair (GKeyFile *key_file,
value_len = line + length - value_start;
value = g_strndup (value_start, value_len);
g_warn_if_fail (key_file->start_group != NULL);
if (key_file->current_group
@ -1407,9 +1405,10 @@ g_key_file_parse_key_value_pair (GKeyFile *key_file,
&& key_file->start_group == key_file->current_group
&& strcmp (key, "Encoding") == 0)
{
if (g_ascii_strcasecmp (value, "UTF-8") != 0)
if (value_len != strlen ("UTF-8") ||
g_ascii_strncasecmp (value_start, "UTF-8", value_len) != 0)
{
gchar *value_utf8 = g_utf8_make_valid (value, value_len);
gchar *value_utf8 = g_utf8_make_valid (value_start, value_len);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
_("Key file contains unsupported "
@ -1417,11 +1416,12 @@ g_key_file_parse_key_value_pair (GKeyFile *key_file,
g_free (value_utf8);
g_free (key);
g_free (value);
return;
}
}
value = g_strndup (value_start, value_len);
/* Is this key a translation? If so, is it one that we care about?
*/
locale = key_get_locale (key);