]> git.tdb.fi Git - libs/core.git/commitdiff
Fix an issue with normalizing paths of the form /foo/..
authorMikko Rasa <tdb@tdb.fi>
Tue, 27 Dec 2022 15:32:24 +0000 (17:32 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 27 Dec 2022 15:32:24 +0000 (17:32 +0200)
The separator indicating the root directory was being incorrectly
removed, resulting in a path denoting the current directory.

source/fs/path.cpp
tests/path.cpp

index 0e86600453be225bcb647ff699f3cb0b5f20649b..1181b923c1054f894c5f0e188119e067c6508566 100644 (file)
@@ -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();
                        }
index fbb5ff54588685527080faa9991ed94b14e4b8fb..d52630a29d4bd72dfe3f1af86d1484834a4f1d24 100644 (file)
@@ -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 = "/..";