Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions llvm/lib/Support/SandboxingFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
using namespace llvm;

// FIXME: Move to llvm/Support/Path.h?
/// \returns true if \p Path is a nested directory in \p ParentPath.
/// \p Path should be absolute. Returns false if \p ParentPath is relative.
/// \returns true if \p Path is a nested directory in \p ParentPath or equal to
/// it. \p Path should be absolute. Returns false if \p ParentPath is relative.
static bool isPathNestedIn(StringRef Path, StringRef ParentPath) {
assert(sys::path::is_absolute(Path));
if (Path.size() <= ParentPath.size())
if (Path.size() < ParentPath.size())
return false;
if (!Path.starts_with(ParentPath))
return false;
if (Path.size() == ParentPath.size())
return true;
return sys::path::is_separator(Path.drop_front(ParentPath.size()).front());
}

Expand Down
2 changes: 2 additions & 0 deletions llvm/unittests/Support/SandboxingFileSystemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ TEST(SandboxingFileSystemTest, Basic) {
vfs::createSandboxingFileSystem(BaseFS, {"//root/dir1", "dir3"})
.moveInto(SandBoxFS),
Succeeded());
EXPECT_TRUE(SandBoxFS->exists("//root/dir1"));
EXPECT_TRUE(SandBoxFS->exists("//root/dir1/"));
EXPECT_TRUE(SandBoxFS->exists("//root/dir1/a1"));
EXPECT_FALSE(SandBoxFS->exists("//root/dir2/a2"));
EXPECT_FALSE(SandBoxFS->exists("//root/dir1a/aa"));
Expand Down