Skip to content

Commit

Permalink
Fix possible infinite loop in fs::is_sub_path
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Dec 27, 2024
1 parent c495a0f commit deee075
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/core/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,15 @@ namespace fs {
while (p != emptyPath) {
fs::error_code ec;
if (equivalent(p, root, ec)) {
printf("%s == %s\n", p.u8string().c_str(), root.u8string().c_str());
return true;
}
p = p.parent_path();
printf("%s != %s\n", p.u8string().c_str(), root.u8string().c_str());
path parent = p.parent_path();
if (parent == p) {
break;
}
p = parent;
}
return false;
}
Expand Down
36 changes: 36 additions & 0 deletions test/core/test_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,50 @@ TEST(FsTest, U8FourBytes) {
EXPECT_EQ(fs::u8path(s).u8string(), s);
}

static fs::path my_realpath(const fs::path& path)
{
fs::path res;
#if !defined WIN32 && !defined _WIN32
char* tmp = realpath(path.c_str(), NULL);
if (tmp) {
res = tmp;
free(tmp);
}
#else
auto tmp = _wfullpath(nullptr, path.c_str(), 1024);
if (tmp) {
auto cmp = [tmp](const fs::path& p) { return wcsicmp(tmp, p.c_str()) == 0; };
res = tmp;
free(tmp);
}
#endif
return res;
}

TEST(FsTest, SubPath) {
EXPECT_TRUE(fs::is_sub_path(fs::path("assets") / "icon.png", "assets"));
}

TEST(FsTest, SubPathAbs) {
EXPECT_TRUE(fs::is_sub_path(my_realpath("assets") / "icon.png", "assets"));
}

TEST(FsTest, SubPathAbsAbs) {
EXPECT_TRUE(fs::is_sub_path(my_realpath("assets") / "icon.png", my_realpath("assets")));
}

TEST(FsTest, NotSubPath) {
EXPECT_FALSE(fs::is_sub_path(fs::path("assets") / "icon.png", "doc"));
}

TEST(FsTest, NotSubPathAbs) {
EXPECT_FALSE(fs::is_sub_path(my_realpath("assets") / "icon.png", "doc"));
}

TEST(FsTest, NotSubPathAbsAbs) {
EXPECT_FALSE(fs::is_sub_path(my_realpath("assets") / "icon.png", my_realpath("doc")));
}

TEST(FsTest, IdenticalSubPath) {
EXPECT_TRUE(fs::is_sub_path(fs::path("assets") / "icon.png", fs::path("assets") / "icon.png"));
}
Expand Down

0 comments on commit deee075

Please sign in to comment.