Merge branch '3098-keyfile-escaping' into 'main'

Revert "gkeyfile: Temporarily re-allow invalid escapes when parsing strings"

Closes #3098

See merge request GNOME/glib!3618
This commit is contained in:
Philip Withnall 2023-10-09 16:50:56 +00:00
commit 240156d174
2 changed files with 8 additions and 18 deletions

View File

@ -4351,7 +4351,6 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
break; break;
case '\0': case '\0':
g_clear_error (error);
g_set_error_literal (error, G_KEY_FILE_ERROR, g_set_error_literal (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE, G_KEY_FILE_ERROR_INVALID_VALUE,
_("Key file contains escape character " _("Key file contains escape character "
@ -4374,25 +4373,11 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
sequence[1] = *p; sequence[1] = *p;
sequence[2] = '\0'; sequence[2] = '\0';
/* FIXME: This should be a fatal error, but there was a
* bug which prevented that being reported for a long
* time, so a lot of applications and in-the-field key
* files use invalid escape sequences without anticipating
* problems. For now (GLib 2.78), message about it; in
* future, the behaviour may become fatal again.
*
* The previous behaviour was to set the #GError but not
* return failure from the function, so the caller could
* explicitly check for invalid escapes, but also ignore
* the error if they want. This is not how #GError is
* meant to be used, but the #GKeyFile code is very old.
*
* See https://gitlab.gnome.org/GNOME/glib/-/issues/3098 */
g_clear_error (error);
g_set_error (error, G_KEY_FILE_ERROR, g_set_error (error, G_KEY_FILE_ERROR,
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;

View File

@ -589,8 +589,9 @@ 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" /* escape at end of line */
"key3=foo\\i\\\n"; "key3=\\ifoo\n" /* invalid escape */
"key4=\\i\\hfoo\n"; /* invalid escape with multiple stacked errors */
keyfile = load_data (data, 0); keyfile = load_data (data, 0);
@ -613,6 +614,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", "key4", &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");