Skip to content

Commit d097c95

Browse files
committed
[Closes #1] fs::canonical now errors on empty path and fs::weakly_canonical doesn't call canonical with empty path anymore
1 parent 58610c4 commit d097c95

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

filesystem.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -2615,6 +2615,10 @@ inline path canonical(const path& p)
26152615

26162616
inline path canonical(const path& p, std::error_code& ec)
26172617
{
2618+
if(p.empty()) {
2619+
ec = detail::make_error_code(detail::portable_error::not_found);
2620+
return path();
2621+
}
26182622
path work = p.is_absolute() ? p : absolute(p, ec);
26192623
path root = work.root_path();
26202624
path result;
@@ -3813,9 +3817,14 @@ inline path weakly_canonical(const path& p, std::error_code& ec) noexcept
38133817
return path();
38143818
}
38153819
scan = false;
3816-
result = canonical(result, ec) / pe;
3817-
if (ec) {
3818-
break;
3820+
if(!result.empty()) {
3821+
result = canonical(result, ec) / pe;
3822+
if (ec) {
3823+
break;
3824+
}
3825+
}
3826+
else {
3827+
result /= pe;
38193828
}
38203829
}
38213830
}
@@ -3824,7 +3833,9 @@ inline path weakly_canonical(const path& p, std::error_code& ec) noexcept
38243833
}
38253834
}
38263835
if (scan) {
3827-
result = canonical(result, ec);
3836+
if(!result.empty()) {
3837+
result = canonical(result, ec);
3838+
}
38283839
}
38293840
return ec ? path() : result.lexically_normal();
38303841
}

test/filesystem_test.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ TEST_CASE("30.10.15.1 absolute", "[filesystem][operations][fs.op.absolute]")
11011101

11021102
TEST_CASE("30.10.15.2 canonical", "[filesystem][operations][fs.op.canonical]")
11031103
{
1104-
CHECK(fs::canonical("") == fs::current_path());
1104+
CHECK_THROWS_AS(fs::canonical(""), fs::filesystem_error);
11051105
CHECK(fs::canonical(fs::current_path()) == fs::current_path());
11061106

11071107
CHECK(fs::canonical(".") == fs::current_path());
@@ -1462,6 +1462,7 @@ TEST_CASE("30.10.15.13 exists", "[filesystem][operations][fs.op.exists]")
14621462
{
14631463
TemporaryDirectory t(TempOpt::change_path);
14641464
std::error_code ec;
1465+
CHECK(!fs::exists(""));
14651466
CHECK(!fs::exists("foo"));
14661467
CHECK(!fs::exists("foo", ec));
14671468
CHECK(!ec);
@@ -2130,9 +2131,9 @@ TEST_CASE("30.10.15.38 temporary_directory_path", "[filesystem][operations][fs.o
21302131

21312132
TEST_CASE("30.10.15.39 weakly_canonical", "[filesystem][operations][fs.op.weakly_canonical]")
21322133
{
2133-
CHECK(fs::weakly_canonical("foo/bar") == fs::current_path() / "foo/bar");
2134-
CHECK(fs::weakly_canonical("foo/./bar") == fs::current_path() / "foo/bar");
2135-
CHECK(fs::weakly_canonical("foo/../bar") == fs::current_path() / "bar");
2134+
CHECK(fs::weakly_canonical("foo/bar") == "foo/bar");
2135+
CHECK(fs::weakly_canonical("foo/./bar") == "foo/bar");
2136+
CHECK(fs::weakly_canonical("foo/../bar") == "bar");
21362137

21372138
{
21382139
TemporaryDirectory t(TempOpt::change_path);

0 commit comments

Comments
 (0)