Check for b value if null in string_append

This avoids "a", NULL, "sep" to come to "asep" instead of "a".
This commit is contained in:
Benedikt Heine 2017-09-25 03:55:19 +02:00
parent 8b11113be1
commit a1781a451b
2 changed files with 4 additions and 0 deletions

View File

@ -78,6 +78,8 @@ char *string_append(char *a, const char *b, const char *sep)
{ {
if (!a) if (!a)
return g_strdup(b); return g_strdup(b);
if (!b)
return a;
char *new; char *new;
if (!sep) if (!sep)

View File

@ -91,6 +91,8 @@ TEST test_string_append(void)
ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), "", NULL))); ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), "", NULL)));
g_free(exp); g_free(exp);
ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), NULL, "_sep_")));
g_free(exp);
ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_"))); ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_")));
g_free(exp); g_free(exp);