From 371f9ad1f8d168497c37039d2ae7916bd55b37c1 Mon Sep 17 00:00:00 2001 From: Mike Gevaert Date: Tue, 5 Nov 2024 13:57:42 +0100 Subject: [PATCH] fix `lexically_normal` for `../foo/../../bar/` * originally this was "normalizing" to `"bar"` when it should be `"../../bar"` * this fixes #185 --- include/ghc/filesystem.hpp | 7 ++++--- test/filesystem_test.cpp | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index 1770d73..542f4ba 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -3261,11 +3261,12 @@ GHC_INLINE path path::lexically_normal() const continue; } else if (s == ".." && !dest.empty()) { - auto root = root_path(); - if (dest == root) { + if (dest == root_path()) { continue; } - else if (*(--dest.end()) != "..") { + + const auto filename = *(--dest.end()); + if (filename != ".." && !(filename.empty() && dest.has_parent_path() && dest.parent_path() == "..")) { if (dest._path.back() == preferred_separator) { dest._path.pop_back(); } diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index 0be179f..9f48e26 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -955,6 +955,7 @@ TEST_CASE("fs.path.gen - path generation", "[filesystem][path][fs.path.gen]") CHECK(fs::path("ab/cd/ef/../../qw").lexically_normal() == "ab/qw"); CHECK(fs::path("a/b/../../../c").lexically_normal() == "../c"); CHECK(fs::path("../").lexically_normal() == ".."); + CHECK(fs::path("../foo/../../bar/").lexically_normal() == "../../bar/"); #ifdef GHC_OS_WINDOWS CHECK(fs::path("\\/\\///\\/").lexically_normal() == "/"); CHECK(fs::path("a/b/..\\//..///\\/../c\\\\/").lexically_normal() == "../c/");