Skip to content

Instantly share code, notes, and snippets.

@jralls
Created September 6, 2022 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jralls/bb3723e46679d37dba39cf01d2465640 to your computer and use it in GitHub Desktop.
Save jralls/bb3723e46679d37dba39cf01d2465640 to your computer and use it in GitHub Desktop.
gnc_g_list_stringjoin: Ignore nulls and empties in string list.
index 7bb2ba5333..fb3602a73d 100644
--- a/libgnucash/core-utils/gnc-glib-utils.c
+++ b/libgnucash/core-utils/gnc-glib-utils.c
@@ -328,6 +328,12 @@ void gnc_gpid_kill(GPid pid)
#endif /* G_OS_WIN32 */
}
+static inline gboolean
+has_string (GList* item)
+{
+ return item && item->data && *(gchar*)item->data != '\0';
+}
+
gchar *
gnc_g_list_stringjoin (GList *list_of_strings, const gchar *sep)
{
@@ -339,14 +345,19 @@ gnc_g_list_stringjoin (GList *list_of_strings, const gchar *sep)
return NULL;
for (GList *n = list_of_strings; n; n = n->next)
- length += strlen ((gchar*)n->data) + seplen;
+ length += has_string(n) ?
+ strlen ((gchar*)n->data) + seplen : 0;
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)
- p = g_stpcpy (p, sep);
+ if (has_string(n))
+ {
+ if (retval && *retval != '\0' && sep)
+ p = g_stpcpy (p, sep);
+
+ p = g_stpcpy (p, (gchar*)n->data);
+ }
}
return retval;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment