diff --git a/src/basic/path-util.c b/src/basic/path-util.c index eb64c886e6..243771d9e4 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -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; diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index b5030ea494..dd00f13905 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -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("/"), "/"));