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,6 +4377,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 invalid escape " _("Key file contains invalid escape "
"sequence “%s”"), sequence); "sequence “%s”"), sequence);
goto error;
} }
} }
break; break;
@ -4384,7 +4388,7 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
*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;
} }
} }
@ -4400,11 +4404,17 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
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

@ -589,7 +589,8 @@ test_string (void)
"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");