]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util-lib: handle empty string in last_path_component 7237/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 Nov 2017 19:54:31 +0000 (20:54 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 Nov 2017 19:54:31 +0000 (20:54 +0100)
Now the function returns an empty string when given an empty string.
Not sure if this is the best option (maybe this should be an error?),
but at least the behaviour is well defined.

src/basic/path-util.c
src/test/test-path-util.c

index 059e4fcf6a51230a2a37614322b75fb3dac6499e..3bde1d1e01bf3ddae470a5f9b2520e95d8965040 100644 (file)
@@ -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--;
 
index 21d52f5d6e6736db3386f1ee610c50cc9d88ddc8..0db835608ad772cd7b576aca81498f5e726c0a55 100644 (file)
@@ -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) {