gkeyfile: Ensure we don't add extra blank line above new group

A forgotten edge case in 86b4b045: when the last value of the last group
has been added via g_key_file_set_value() and it contains line breaks.
The best we can do in this case is probably to do nothing.

Closes: #3047
Fixes: 86b4b0453e
This commit is contained in:
Gaël Bonithon 2023-07-13 10:06:21 +02:00
parent 4aa63defdf
commit c49502582f
2 changed files with 15 additions and 1 deletions

View File

@ -3858,8 +3858,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;

View File

@ -480,6 +480,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);
}