Merge branch 'keyfile-escape-fix' into 'main'

gkeyfile: Fix overwriting of GError

See merge request GNOME/glib!3555
This commit is contained in:
Philip Withnall 2023-08-31 10:04:50 +00:00
commit ff0eea48ae
2 changed files with 41 additions and 26 deletions

View File

@ -4313,8 +4313,11 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
GError **error) GError **error)
{ {
gchar *string_value, *q0, *q; gchar *string_value, *q0, *q;
GSList *tmp_pieces = NULL;
const gchar *p; const gchar *p;
g_assert (pieces == NULL || *pieces == NULL);
string_value = g_new (gchar, strlen (value) + 1); string_value = g_new (gchar, strlen (value) + 1);
p = value; p = value;
@ -4352,7 +4355,7 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
G_KEY_FILE_ERROR_INVALID_VALUE, G_KEY_FILE_ERROR_INVALID_VALUE,
_("Key file contains escape character " _("Key file contains escape character "
"at end of line")); "at end of line"));
break; goto error;
default: default:
if (pieces && *p == key_file->list_separator) if (pieces && *p == key_file->list_separator)
@ -4374,20 +4377,21 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
G_KEY_FILE_ERROR_INVALID_VALUE, G_KEY_FILE_ERROR_INVALID_VALUE,
_("Key file contains invalid escape " _("Key file contains invalid escape "
"sequence “%s”"), sequence); "sequence “%s”"), sequence);
} goto error;
} }
}
break; break;
} }
} }
else else
{ {
*q = *p; *q = *p;
if (pieces && (*p == key_file->list_separator)) if (pieces && (*p == key_file->list_separator))
{ {
*pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0)); tmp_pieces = g_slist_prepend (tmp_pieces, g_strndup (q0, q - q0));
q0 = q + 1; q0 = q + 1;
} }
} }
if (*p == '\0') if (*p == '\0')
break; break;
@ -4398,13 +4402,19 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
*q = '\0'; *q = '\0';
if (pieces) if (pieces)
{ {
if (q0 < q) if (q0 < q)
*pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0)); tmp_pieces = g_slist_prepend (tmp_pieces, g_strndup (q0, q - q0));
*pieces = g_slist_reverse (*pieces); *pieces = g_slist_reverse (tmp_pieces);
} }
return string_value; return string_value;
error:
g_free (string_value);
g_slist_free_full (tmp_pieces, g_free);
return NULL;
} }
static gchar * static gchar *

View File

@ -580,16 +580,17 @@ test_string (void)
"3", "3",
}; };
const gchar *data = const gchar *data =
"[valid]\n" "[valid]\n"
"key1=\\s\\n\\t\\r\\\\\n" "key1=\\s\\n\\t\\r\\\\\n"
"key2=\"quoted\"\n" "key2=\"quoted\"\n"
"key3='quoted'\n" "key3='quoted'\n"
"key4=\xe2\x89\xa0\xe2\x89\xa0\n" "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
"key5= leading space\n" "key5= leading space\n"
"key6=trailing space \n" "key6=trailing space \n"
"[invalid]\n" "[invalid]\n"
"key1=\\a\\b\\0800xff\n" "key1=\\a\\b\\0800xff\n"
"key2=blabla\\\n"; "key2=blabla\\\n"
"key3=foo\\i\\\n";
keyfile = load_data (data, 0); keyfile = load_data (data, 0);
@ -608,6 +609,10 @@ test_string (void)
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE); check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
g_free (value); g_free (value);
value = g_key_file_get_string (keyfile, "invalid", "key3", &error);
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
g_free (value);
g_key_file_set_string (keyfile, "inserted", "key1", "simple"); g_key_file_set_string (keyfile, "inserted", "key1", "simple");
g_key_file_set_string (keyfile, "inserted", "key2", " leading space"); g_key_file_set_string (keyfile, "inserted", "key2", " leading space");
g_key_file_set_string (keyfile, "inserted", "key3", "\tleading tab"); g_key_file_set_string (keyfile, "inserted", "key3", "\tleading tab");