From dbb2e02721b6250a434a807d92126c7716c40a15 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 27 Dec 2022 17:32:24 +0200 Subject: [PATCH] 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. --- source/fs/path.cpp | 14 ++++++++++++-- tests/path.cpp | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) 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 = "/.."; -- 2.45.2