1From 51dfb3c229c0478b3615f486fbbc36de2586bd52 Mon Sep 17 00:00:00 2001 2From: =?UTF-8?q?Ga=C3=ABl=20Bonithon?= <gael@xfce.org> 3Date: Thu, 13 Jul 2023 10:19:04 +0200 4Subject: [PATCH] gkeyfile: Skip group comment when adding a new key to a group 5 6An oversight in 86b4b045: since the comment of group N now consists of 7the last null-key values of group N-1, these keys must obviously be 8skipped when adding a new non-null key to group N-1. 9 10Closes: #3047 11Fixes: 86b4b0453ea3a814167d4a5f7a4031d467543716 12--- 13 glib/gkeyfile.c | 19 ++++++++++++++----- 14 glib/tests/keyfile.c | 9 +++++++++ 15 2 files changed, 23 insertions(+), 5 deletions(-) 16 17diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c 18index 0e21ab4f14..4759051977 100644 19--- a/glib/gkeyfile.c 20+++ b/glib/gkeyfile.c 21@@ -573,7 +573,8 @@ static void g_key_file_remove_key_value_pair_node (GKeyFile 22 23 static void g_key_file_add_key_value_pair (GKeyFile *key_file, 24 GKeyFileGroup *group, 25- GKeyFileKeyValuePair *pair); 26+ GKeyFileKeyValuePair *pair, 27+ GList *sibling); 28 static void g_key_file_add_key (GKeyFile *key_file, 29 GKeyFileGroup *group, 30 const gchar *key, 31@@ -1447,7 +1448,8 @@ g_key_file_parse_key_value_pair (GKeyFile *key_file, 32 pair->key = g_steal_pointer (&key); 33 pair->value = g_strndup (value_start, value_len); 34 35- g_key_file_add_key_value_pair (key_file, key_file->current_group, pair); 36+ g_key_file_add_key_value_pair (key_file, key_file->current_group, pair, 37+ key_file->current_group->key_value_pairs); 38 } 39 40 g_free (key); 41@@ -4034,10 +4036,11 @@ g_key_file_remove_group (GKeyFile *key_file, 42 static void 43 g_key_file_add_key_value_pair (GKeyFile *key_file, 44 GKeyFileGroup *group, 45- GKeyFileKeyValuePair *pair) 46+ GKeyFileKeyValuePair *pair, 47+ GList *sibling) 48 { 49 g_hash_table_replace (group->lookup_map, pair->key, pair); 50- group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair); 51+ group->key_value_pairs = g_list_insert_before (group->key_value_pairs, sibling, pair); 52 } 53 54 static void 55@@ -4047,12 +4050,18 @@ g_key_file_add_key (GKeyFile *key_file, 56 const gchar *value) 57 { 58 GKeyFileKeyValuePair *pair; 59+ GList *lp; 60 61 pair = g_new (GKeyFileKeyValuePair, 1); 62 pair->key = g_strdup (key); 63 pair->value = g_strdup (value); 64 65- g_key_file_add_key_value_pair (key_file, group, pair); 66+ /* skip group comment */ 67+ lp = group->key_value_pairs; 68+ while (lp != NULL && ((GKeyFileKeyValuePair *) lp->data)->key == NULL) 69+ lp = lp->next; 70+ 71+ g_key_file_add_key_value_pair (key_file, group, pair, lp); 72 } 73 74 /** 75diff --git a/glib/tests/keyfile.c b/glib/tests/keyfile.c 76index 80cdc93d8f..2c8eca4ebc 100644 77--- a/glib/tests/keyfile.c 78+++ b/glib/tests/keyfile.c 79@@ -456,6 +456,15 @@ test_comments (void) 80 check_name ("group comment", comment, group_comment, 0); 81 g_free (comment); 82 83+ g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3047"); 84+ 85+ /* check if adding a key to group N preserve group comment of group N+1 */ 86+ g_key_file_set_string (keyfile, "group1", "key5", "value5"); 87+ comment = g_key_file_get_comment (keyfile, "group2", NULL, &error); 88+ check_no_error (&error); 89+ check_name ("group comment", comment, group_comment, 0); 90+ g_free (comment); 91+ 92 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/104"); 93 94 /* check if comments above another group than the first one are properly removed */ 95-- 96GitLab 97 98