• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From c49502582faedecc7020155d95b16c7a1d78d432 Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Ga=C3=ABl=20Bonithon?= <gael@xfce.org>
3Date: Thu, 13 Jul 2023 10:06:21 +0200
4Subject: [PATCH] gkeyfile: Ensure we don't add extra blank line above new
5 group
6
7A forgotten edge case in 86b4b045: when the last value of the last group
8has been added via g_key_file_set_value() and it contains line breaks.
9The best we can do in this case is probably to do nothing.
10
11Closes: #3047
12Fixes: 86b4b0453ea3a814167d4a5f7a4031d467543716
13---
14 glib/gkeyfile.c      |  6 +++++-
15 glib/tests/keyfile.c | 10 ++++++++++
16 2 files changed, 15 insertions(+), 1 deletion(-)
17
18diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c
19index 145136706f..0e21ab4f14 100644
20--- a/glib/gkeyfile.c
21+++ b/glib/gkeyfile.c
22@@ -3858,8 +3858,12 @@ g_key_file_add_group (GKeyFile    *key_file,
23     {
24       /* separate groups by a blank line if we don't keep comments or group is created */
25       GKeyFileGroup *next_group = key_file->groups->next->data;
26+      GKeyFileKeyValuePair *pair;
27+      if (next_group->key_value_pairs != NULL)
28+        pair = next_group->key_value_pairs->data;
29+
30       if (next_group->key_value_pairs == NULL ||
31-          ((GKeyFileKeyValuePair *) next_group->key_value_pairs->data)->key != NULL)
32+          (pair->key != NULL && !g_strstr_len (pair->value, -1, "\n")))
33         {
34           GKeyFileKeyValuePair *pair = g_new (GKeyFileKeyValuePair, 1);
35           pair->key = NULL;
36diff --git a/glib/tests/keyfile.c b/glib/tests/keyfile.c
37index d3eed29841..80cdc93d8f 100644
38--- a/glib/tests/keyfile.c
39+++ b/glib/tests/keyfile.c
40@@ -480,6 +480,16 @@ test_comments (void)
41                G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
42   g_assert_null (comment);
43
44+  g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3047");
45+
46+  /* check if we don't add a blank line above new group if last value of preceding
47+   * group was added via g_key_file_set_value() and contains line breaks */
48+  g_key_file_set_value (keyfile, "group4", "key1", "value1\n\n# group comment");
49+  g_key_file_set_string (keyfile, "group5", "key1", "value1");
50+  comment = g_key_file_get_comment (keyfile, "group5", NULL, &error);
51+  check_no_error (&error);
52+  g_assert_null (comment);
53+
54   g_key_file_free (keyfile);
55 }
56
57--
58GitLab
59
60