Merge branch 'keyfile-regression-3047' into 'main'

gkeyfile: Fix regression #3047 (group comment)

Closes #3047

See merge request GNOME/glib!3498
This commit is contained in:
Philip Withnall 2023-07-20 11:59:07 +00:00
commit b16e682112
2 changed files with 38 additions and 6 deletions

View File

@ -573,7 +573,8 @@ static void g_key_file_remove_key_value_pair_node (GKeyFile
static void g_key_file_add_key_value_pair (GKeyFile *key_file,
GKeyFileGroup *group,
GKeyFileKeyValuePair *pair);
GKeyFileKeyValuePair *pair,
GList *sibling);
static void g_key_file_add_key (GKeyFile *key_file,
GKeyFileGroup *group,
const gchar *key,
@ -1447,7 +1448,8 @@ g_key_file_parse_key_value_pair (GKeyFile *key_file,
pair->key = g_steal_pointer (&key);
pair->value = g_strndup (value_start, value_len);
g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
g_key_file_add_key_value_pair (key_file, key_file->current_group, pair,
key_file->current_group->key_value_pairs);
}
g_free (key);
@ -3858,8 +3860,12 @@ g_key_file_add_group (GKeyFile *key_file,
{
/* separate groups by a blank line if we don't keep comments or group is created */
GKeyFileGroup *next_group = key_file->groups->next->data;
GKeyFileKeyValuePair *pair;
if (next_group->key_value_pairs != NULL)
pair = next_group->key_value_pairs->data;
if (next_group->key_value_pairs == NULL ||
((GKeyFileKeyValuePair *) next_group->key_value_pairs->data)->key != NULL)
(pair->key != NULL && !g_strstr_len (pair->value, -1, "\n")))
{
GKeyFileKeyValuePair *pair = g_new (GKeyFileKeyValuePair, 1);
pair->key = NULL;
@ -4030,10 +4036,11 @@ g_key_file_remove_group (GKeyFile *key_file,
static void
g_key_file_add_key_value_pair (GKeyFile *key_file,
GKeyFileGroup *group,
GKeyFileKeyValuePair *pair)
GKeyFileKeyValuePair *pair,
GList *sibling)
{
g_hash_table_replace (group->lookup_map, pair->key, pair);
group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
group->key_value_pairs = g_list_insert_before (group->key_value_pairs, sibling, pair);
}
static void
@ -4043,12 +4050,18 @@ g_key_file_add_key (GKeyFile *key_file,
const gchar *value)
{
GKeyFileKeyValuePair *pair;
GList *lp;
pair = g_new (GKeyFileKeyValuePair, 1);
pair->key = g_strdup (key);
pair->value = g_strdup (value);
g_key_file_add_key_value_pair (key_file, group, pair);
/* skip group comment */
lp = group->key_value_pairs;
while (lp != NULL && ((GKeyFileKeyValuePair *) lp->data)->key == NULL)
lp = lp->next;
g_key_file_add_key_value_pair (key_file, group, pair, lp);
}
/**

View File

@ -456,6 +456,15 @@ test_comments (void)
check_name ("group comment", comment, group_comment, 0);
g_free (comment);
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3047");
/* check if adding a key to group N preserve group comment of group N+1 */
g_key_file_set_string (keyfile, "group1", "key5", "value5");
comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
check_no_error (&error);
check_name ("group comment", comment, group_comment, 0);
g_free (comment);
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/104");
/* check if comments above another group than the first one are properly removed */
@ -480,6 +489,16 @@ test_comments (void)
G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
g_assert_null (comment);
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3047");
/* check if we don't add a blank line above new group if last value of preceding
* group was added via g_key_file_set_value() and contains line breaks */
g_key_file_set_value (keyfile, "group4", "key1", "value1\n\n# group comment");
g_key_file_set_string (keyfile, "group5", "key1", "value1");
comment = g_key_file_get_comment (keyfile, "group5", NULL, &error);
check_no_error (&error);
g_assert_null (comment);
g_key_file_free (keyfile);
}