Skip to content

Commit

Permalink
[Closes #1] fs::canonical now errors on empty path and fs::weakly_can…
Browse files Browse the repository at this point in the history
…onical doesn't call canonical with empty path anymore
  • Loading branch information
gulrak committed Sep 23, 2018
1 parent 58610c4 commit d097c95
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
19 changes: 15 additions & 4 deletions filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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();
}
Expand Down
9 changes: 5 additions & 4 deletions test/filesystem_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d097c95

Please sign in to comment.