]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
path-util: fix off by one issue to detect slash at the end in path_extend()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 28 May 2021 04:38:31 +0000 (13:38 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 28 May 2021 04:41:23 +0000 (13:41 +0900)
src/basic/path-util.c
src/test/test-path-util.c

index 28b2c66b6efdb5549cc500cd8121ebca1f768933..08dace775dcd176af60a51e35461d0e680547cd5 100644 (file)
@@ -597,7 +597,7 @@ char* path_extend_internal(char **x, ...) {
                 *x = nx;
 
         if (old_sz > 0)
-                slash = nx[old_sz] == '/';
+                slash = nx[old_sz-1] == '/';
         else {
                 nx[old_sz] = 0;
                 slash = true; /* no need to generate a slash anymore */
index 8091a301e38496c6e6ed2fee8549c0685cbe379a..c93a21a4754754b941214bd0c8e5424903017d5d 100644 (file)
@@ -397,8 +397,14 @@ static void test_path_extend(void) {
 
         assert_se(path_extend(&p, "/foo") == p);
         assert_se(streq(p, "foo/foo"));
-        assert_se(path_extend(&p, "waaaah/wahhh/") == p);
-        assert_se(streq(p, "foo/foo/waaaah/wahhh/"));
+        assert_se(path_extend(&p, "/waaaah/wahhh//") == p);
+        assert_se(streq(p, "foo/foo/waaaah/wahhh//")); /* path_extend() does not drop redundant slashes */
+        assert_se(path_extend(&p, "/aaa/bbb/") == p);
+        assert_se(streq(p, "foo/foo/waaaah/wahhh///aaa/bbb/")); /* but not add an extra slash */
+
+        assert_se(free_and_strdup(&p, "/") >= 0);
+        assert_se(path_extend(&p, "foo") == p);
+        assert_se(streq(p, "/foo"));
 }
 
 static void test_fsck_exists(void) {