diff --git a/llvm/lib/Support/SandboxingFileSystem.cpp b/llvm/lib/Support/SandboxingFileSystem.cpp index 34c1d96efd5e9..16474b388131e 100644 --- a/llvm/lib/Support/SandboxingFileSystem.cpp +++ b/llvm/lib/Support/SandboxingFileSystem.cpp @@ -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()); } diff --git a/llvm/unittests/Support/SandboxingFileSystemTest.cpp b/llvm/unittests/Support/SandboxingFileSystemTest.cpp index 6799d9511484f..e22db390ba8c0 100644 --- a/llvm/unittests/Support/SandboxingFileSystemTest.cpp +++ b/llvm/unittests/Support/SandboxingFileSystemTest.cpp @@ -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"));