From 340cd6b6f9c4067501c2e3f85e05b55e779cb8ea Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 28 May 2021 13:38:31 +0900 Subject: [PATCH] path-util: fix off by one issue to detect slash at the end in path_extend() --- src/basic/path-util.c | 2 +- src/test/test-path-util.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/basic/path-util.c b/src/basic/path-util.c index 28b2c66b6ef..08dace775dc 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -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 */ diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 8091a301e38..c93a21a4754 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -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) { -- 2.47.3