diff --git a/src/basic/path-util.c b/src/basic/path-util.c index 059e4fcf6a..3bde1d1e01 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -719,6 +719,9 @@ const char *last_path_component(const char *path) { unsigned l, k; l = k = strlen(path); + if (l == 0) /* special case — an empty string */ + return path; + while (k > 0 && path[k-1] == '/') k--; diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 21d52f5d6e..0db835608a 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -411,6 +411,7 @@ static void test_last_path_component(void) { assert_se(streq(last_path_component("././/"), ".//")); assert_se(streq(last_path_component("/foo/a"), "a")); assert_se(streq(last_path_component("/foo/a/"), "a/")); + assert_se(streq(last_path_component(""), "")); } static void test_filename_is_valid(void) {