[gnc-glib-utils] gnc_g_list_stringjoin skips NULL data

This commit is contained in:
Christopher Lam 2022-09-06 09:21:52 +08:00
parent 0f13471438
commit ecd2cdf425
2 changed files with 18 additions and 6 deletions

View File

@ -335,18 +335,25 @@ gnc_g_list_stringjoin (GList *list_of_strings, const gchar *sep)
gint length = -seplen;
gchar *retval, *p;
if (!list_of_strings)
return NULL;
for (GList *n = list_of_strings; n; n = n->next)
length += strlen ((gchar*)n->data) + seplen;
{
gchar *str = n->data;
if (str && *str)
length += strlen (str) + seplen;
}
if (length <= 0)
return NULL;
p = retval = (gchar*) g_malloc0 (length * sizeof (gchar) + 1);
for (GList *n = list_of_strings; n; n = n->next)
{
p = g_stpcpy (p, (gchar*)n->data);
if (n->next && sep)
gchar *str = n->data;
if (!str || !str[0])
continue;
if (sep && (p != retval))
p = g_stpcpy (p, sep);
p = g_stpcpy (p, str);
}
return retval;

View File

@ -79,6 +79,11 @@ test_g_list_stringjoin (gconstpointer data)
g_assert_cmpstr (ret, ==, "one");
g_free (ret);
/* The following inserts a NULL between "two" and "one". As a
result, the stringjoin effectively skips a step, i.e. it does
not insert separator repeatedly between NULL strings */
test = g_list_prepend (test, NULL);
test = g_list_prepend (test, "two");
ret = gnc_g_list_stringjoin (test, NULL);