From d097c9535a4d7a9053335d76329b95e25345c81e Mon Sep 17 00:00:00 2001 From: Steffen Schuemann Date: Sun, 23 Sep 2018 14:12:42 +0200 Subject: [PATCH] [Closes #1] fs::canonical now errors on empty path and fs::weakly_canonical doesn't call canonical with empty path anymore --- filesystem.h | 19 +++++++++++++++---- test/filesystem_test.cpp | 9 +++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/filesystem.h b/filesystem.h index 6ce1c80..ee423a2 100644 --- a/filesystem.h +++ b/filesystem.h @@ -2615,6 +2615,10 @@ inline path canonical(const path& p) inline path canonical(const path& p, std::error_code& ec) { + if(p.empty()) { + ec = detail::make_error_code(detail::portable_error::not_found); + return path(); + } path work = p.is_absolute() ? p : absolute(p, ec); path root = work.root_path(); path result; @@ -3813,9 +3817,14 @@ inline path weakly_canonical(const path& p, std::error_code& ec) noexcept return path(); } scan = false; - result = canonical(result, ec) / pe; - if (ec) { - break; + if(!result.empty()) { + result = canonical(result, ec) / pe; + if (ec) { + break; + } + } + else { + result /= pe; } } } @@ -3824,7 +3833,9 @@ inline path weakly_canonical(const path& p, std::error_code& ec) noexcept } } if (scan) { - result = canonical(result, ec); + if(!result.empty()) { + result = canonical(result, ec); + } } return ec ? path() : result.lexically_normal(); } diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index f895de0..8c4efdf 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -1101,7 +1101,7 @@ TEST_CASE("30.10.15.1 absolute", "[filesystem][operations][fs.op.absolute]") TEST_CASE("30.10.15.2 canonical", "[filesystem][operations][fs.op.canonical]") { - CHECK(fs::canonical("") == fs::current_path()); + CHECK_THROWS_AS(fs::canonical(""), fs::filesystem_error); CHECK(fs::canonical(fs::current_path()) == fs::current_path()); CHECK(fs::canonical(".") == fs::current_path()); @@ -1462,6 +1462,7 @@ TEST_CASE("30.10.15.13 exists", "[filesystem][operations][fs.op.exists]") { TemporaryDirectory t(TempOpt::change_path); std::error_code ec; + CHECK(!fs::exists("")); CHECK(!fs::exists("foo")); CHECK(!fs::exists("foo", ec)); CHECK(!ec); @@ -2130,9 +2131,9 @@ TEST_CASE("30.10.15.38 temporary_directory_path", "[filesystem][operations][fs.o TEST_CASE("30.10.15.39 weakly_canonical", "[filesystem][operations][fs.op.weakly_canonical]") { - CHECK(fs::weakly_canonical("foo/bar") == fs::current_path() / "foo/bar"); - CHECK(fs::weakly_canonical("foo/./bar") == fs::current_path() / "foo/bar"); - CHECK(fs::weakly_canonical("foo/../bar") == fs::current_path() / "bar"); + CHECK(fs::weakly_canonical("foo/bar") == "foo/bar"); + CHECK(fs::weakly_canonical("foo/./bar") == "foo/bar"); + CHECK(fs::weakly_canonical("foo/../bar") == "bar"); { TemporaryDirectory t(TempOpt::change_path);