diff --git a/src/utils.c b/src/utils.c index 094a748..d838c53 100644 --- a/src/utils.c +++ b/src/utils.c @@ -76,9 +76,11 @@ char *string_replace_all(const char *needle, const char *replacement, char *string_append(char *a, const char *b, const char *sep) { - if (!a) + if (!a || *a == '\0') { + g_free(a); return g_strdup(b); - if (!b) + } + if (!b || *b == '\0') return a; char *new; diff --git a/test/utils.c b/test/utils.c index 6fcee97..f0b7969 100644 --- a/test/utils.c +++ b/test/utils.c @@ -83,6 +83,8 @@ TEST test_string_append(void) ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", NULL))); g_free(exp); + ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", "_sep_"))); + g_free(exp); ASSERT_STR_EQ("b", (exp = string_append(g_strdup(""), "b", NULL))); g_free(exp); @@ -94,6 +96,8 @@ TEST test_string_append(void) ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), NULL, "_sep_"))); g_free(exp); + ASSERT_STR_EQ("", (exp = string_append(g_strdup(""), "", "_sep_"))); + g_free(exp); ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_"))); g_free(exp);