GKeyFile: allow loading from empty strings

GKeyFile supports empty files and also supports loading from the string
"", but will fail with a critical if you try:

  - explicit length == 0
  - data == NULL

length == 0 should always be valid, and data == NULL should be valid in
the case that length == 0, so add some testcases for those and fix the
code up to allow them.

https://bugzilla.gnome.org/show_bug.cgi?id=668756
This commit is contained in:
Ryan Lortie 2012-01-26 11:27:47 -05:00
parent bc40fe582d
commit 9bfde4a53f
2 changed files with 28 additions and 4 deletions

View File

@ -885,8 +885,7 @@ g_key_file_load_from_data (GKeyFile *key_file,
gchar list_separator;
g_return_val_if_fail (key_file != NULL, FALSE);
g_return_val_if_fail (data != NULL, FALSE);
g_return_val_if_fail (length != 0, FALSE);
g_return_val_if_fail (data != NULL || length == 0, FALSE);
if (length == (gsize)-1)
length = strlen (data);
@ -1354,7 +1353,7 @@ g_key_file_parse_data (GKeyFile *key_file,
gsize i;
g_return_if_fail (key_file != NULL);
g_return_if_fail (data != NULL);
g_return_if_fail (data != NULL || length == 0);
parse_error = NULL;

View File

@ -1455,6 +1455,31 @@ test_list_separator (void)
g_key_file_unref (keyfile);
}
static void
test_empty_string (void)
{
GError *error = NULL;
GKeyFile *kf;
kf = g_key_file_new ();
g_key_file_load_from_data (kf, "", 0, 0, &error);
g_assert_no_error (error);
g_key_file_load_from_data (kf, "", -1, 0, &error);
g_assert_no_error (error);
/* NULL is a fine pointer to use if length is zero */
g_key_file_load_from_data (kf, NULL, 0, 0, &error);
g_assert_no_error (error);
/* should not attempt to access non-NULL pointer if length is zero */
g_key_file_load_from_data (kf, GINT_TO_POINTER (1), 0, 0, &error);
g_assert_no_error (error);
g_key_file_unref (kf);
}
int
main (int argc, char *argv[])
{
@ -1489,7 +1514,7 @@ main (int argc, char *argv[])
g_test_add_func ("/keyfile/ref", test_ref);
g_test_add_func ("/keyfile/replace-value", test_replace_value);
g_test_add_func ("/keyfile/list-separator", test_list_separator);
g_test_add_func ("/keyfile/empty-string", test_empty_string);
return g_test_run ();
}