From: Mikko Rasa Date: Tue, 27 Dec 2022 15:32:24 +0000 (+0200) Subject: Fix an issue with normalizing paths of the form /foo/.. X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=dbb2e02721b6250a434a807d92126c7716c40a15;p=libs%2Fcore.git Fix an issue with normalizing paths of the form /foo/.. The separator indicating the root directory was being incorrectly removed, resulting in a path denoting the current directory. --- diff --git a/source/fs/path.cpp b/source/fs/path.cpp index 0e86600..1181b92 100644 --- a/source/fs/path.cpp +++ b/source/fs/path.cpp @@ -133,11 +133,21 @@ void Path::add_component(const string &comp) path += DIRSEP; path += comp; } - else if(separators.empty()) + else if(start==0) + { + /* Removing the last component of a relative path results in the + current directory */ path = "."; + } + else if(start==1) + { + /* Removing the last component of an absolute path results in the + root directory */ + path.erase(start, string::npos); + } else { - // Otherwise, erase the last component + // Otherwise, erase the last component and its separator path.erase(separators.back(), string::npos); separators.pop_back(); } diff --git a/tests/path.cpp b/tests/path.cpp index fbb5ff5..d52630a 100644 --- a/tests/path.cpp +++ b/tests/path.cpp @@ -52,6 +52,8 @@ void PathTests::normalization() EXPECT_EQUAL(path.str(), "./foo"); path = "foo/.."; EXPECT_EQUAL(path.str(), "."); + path = "/foo/.."; + EXPECT_EQUAL(path.str(), "/"); path = "//foo"; EXPECT_EQUAL(path.str(), "/foo"); path = "/..";