diff --git a/libgnucash/core-utils/gnc-glib-utils.c b/libgnucash/core-utils/gnc-glib-utils.c index 7bb2ba5333..4c10c6faed 100644 --- a/libgnucash/core-utils/gnc-glib-utils.c +++ b/libgnucash/core-utils/gnc-glib-utils.c @@ -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; diff --git a/libgnucash/core-utils/test/test-gnc-glib-utils.c b/libgnucash/core-utils/test/test-gnc-glib-utils.c index 0c59356e30..a350cfaf58 100644 --- a/libgnucash/core-utils/test/test-gnc-glib-utils.c +++ b/libgnucash/core-utils/test/test-gnc-glib-utils.c @@ -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);