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