path-util: handle NULL inputs in last_path_component()

This commit is contained in:
Lennart Poettering 2018-10-26 16:06:54 +02:00
parent 5f7ecd610c
commit 77e0a1b5e0
2 changed files with 4 additions and 0 deletions

View File

@ -750,6 +750,9 @@ const char *last_path_component(const char *path) {
unsigned l, k;
if (!path)
return NULL;
l = k = strlen(path);
if (l == 0) /* special case — an empty string */
return path;

View File

@ -406,6 +406,7 @@ static void test_file_in_same_dir(void) {
}
static void test_last_path_component(void) {
assert_se(last_path_component(NULL) == NULL);
assert_se(streq(last_path_component("a/b/c"), "c"));
assert_se(streq(last_path_component("a/b/c/"), "c/"));
assert_se(streq(last_path_component("/"), "/"));