util: add null string test handling to strv_join

Make it return NULL for a string array in the form of [ NULL ], like the docs
say. This also adds an extra safety check for the joiner to be of a reasonable
length to avoid overflows.

Found in
https://gitlab.freedesktop.org/libinput/libinput/issues/26#note_6320

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2018-06-11 10:45:58 +10:00
parent 46b64c7363
commit 6ad928a1b6
2 changed files with 7 additions and 0 deletions

View file

@ -586,11 +586,15 @@ strv_join(char **strv, const char *joiner)
if (!strv || !joiner)
return NULL;
if (strv[0] == NULL)
return NULL;
for (s = strv, count = 0; *s; s++, count++) {
slen += strlen(*s);
}
assert(slen < 1000);
assert(strlen(joiner) < 1000);
slen += (count - 1) * strlen(joiner);

View file

@ -1366,6 +1366,7 @@ START_TEST(strjoin_test)
{ { NULL }, NULL, NULL }
};
struct strjoin_test *t = tests;
struct strjoin_test nulltest = { {NULL}, "x", NULL };
while (t->strv[0]) {
char *str;
@ -1377,6 +1378,8 @@ START_TEST(strjoin_test)
free(str);
t++;
}
ck_assert(strv_join(nulltest.strv, "x") == NULL);
}
END_TEST