From: Thomas Haller Date: Fri, 5 Oct 2018 11:14:38 +0000 (+0200) Subject: path-util: fix path_simplify() with kill_dots and "." X-Git-Tag: v240~622 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=afbae3e9f23dc6682d48a1cc3585e8429ef07d8b;p=thirdparty%2Fsystemd.git path-util: fix path_simplify() with kill_dots and "." Previously, together with kill_dots true, patch like ".", "./.", ".//.//" would all return an empty string. That is wrong. There must be one "." left to reference the current directory. Also, the comment with examples was wrong. --- diff --git a/src/basic/path-util.c b/src/basic/path-util.c index f3c6c16aae5..f60cf003e75 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -333,12 +333,15 @@ char *path_simplify(char *path, bool kill_dots) { /* Removes redundant inner and trailing slashes. Also removes unnecessary dots * if kill_dots is true. Modifies the passed string in-place. * - * ///foo//./bar/. becomes /foo/./bar/. (if kill_dots is false) - * ///foo//./bar/. becomes /foo/bar (if kill_dots is true) - * .//./foo//./bar/. becomes ./foo/bar (if kill_dots is false) - * .//./foo//./bar/. becomes foo/bar (if kill_dots is true) + * ///foo//./bar/. becomes /foo/./bar/. (if kill_dots is false) + * ///foo//./bar/. becomes /foo/bar (if kill_dots is true) + * .//./foo//./bar/. becomes ././foo/./bar/. (if kill_dots is false) + * .//./foo//./bar/. becomes foo/bar (if kill_dots is true) */ + if (isempty(path)) + return path; + absolute = path_is_absolute(path); f = path; @@ -368,9 +371,14 @@ char *path_simplify(char *path, bool kill_dots) { *(t++) = *f; } - /* Special rule, if we are talking of the root directory, a trailing slash is good */ - if (absolute && t == path) - *(t++) = '/'; + /* Special rule, if we stripped everything, we either need a "/" (for the root directory) + * or "." for the current directory */ + if (t == path) { + if (absolute) + *(t++) = '/'; + else + *(t++) = '.'; + } *t = 0; return path; diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 9ec42aae7b0..8b9686d50be 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -81,10 +81,10 @@ static void test_path(void) { test_path_simplify("///.//", "/.", "/"); test_path_simplify("///.//.///", "/./.", "/"); test_path_simplify("////.././///../.", "/.././../.", "/../.."); - test_path_simplify(".", ".", ""); - test_path_simplify("./", ".", ""); - test_path_simplify(".///.//./.", "./././.", ""); - test_path_simplify(".///.//././/", "./././.", ""); + test_path_simplify(".", ".", "."); + test_path_simplify("./", ".", "."); + test_path_simplify(".///.//./.", "./././.", "."); + test_path_simplify(".///.//././/", "./././.", "."); test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", "/./aaa/././.bbb/../c./d.dd/..eeee/.", "/aaa/.bbb/../c./d.dd/..eeee");