[llvm] [clang] Fix handling of adding a file with the same name as an existing dir to VFS (PR #94461)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 5 04:42:28 PDT 2024
https://github.com/jensmassberg created https://github.com/llvm/llvm-project/pull/94461
When trying to add a file to clang's VFS via `addFile` and a directory of the same name already exists, we run into a [out-of-bound access](https://github.com/llvm/llvm-project/blob/145815c180fc82c5a55bf568d01d98d250490a55/llvm/lib/Support/Path.cpp#L244).
The problem is that the file name is [recognised as existing path]( https://github.com/llvm/llvm-project/blob/145815c180fc82c5a55bf568d01d98d250490a55/llvm/lib/Support/VirtualFileSystem.cpp#L896) and thus continues to process the next part of the path which doesn't exist.
This patch adds a check if we have reached the last part of the filename and return false in that case.
This we reject to add a file if a directory of the same name already exists.
This is in sync with [this check](https://github.com/llvm/llvm-project/blob/145815c180fc82c5a55bf568d01d98d250490a55/llvm/lib/Support/VirtualFileSystem.cpp#L903) that rejects adding a path if a file of the same name already exists.
>From 161365c534f4b5fb196c716544350790c86b061e Mon Sep 17 00:00:00 2001
From: jensmassberg <massberg at google.com>
Date: Wed, 5 Jun 2024 13:19:38 +0200
Subject: [PATCH] [clang] Fix handling of adding a file with the same name as
an existing dir to VFS
---
llvm/lib/Support/VirtualFileSystem.cpp | 4 ++++
llvm/unittests/Support/VirtualFileSystemTest.cpp | 2 ++
2 files changed, 6 insertions(+)
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index fcefdef992be5..defe7a7f45f44 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -893,6 +893,10 @@ bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
}
if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
+ // Trying to insert a file in place of a directory.
+ if (I == E)
+ return false;
+
Dir = NewDir;
} else {
assert((isa<detail::InMemoryFile>(Node) ||
diff --git a/llvm/unittests/Support/VirtualFileSystemTest.cpp b/llvm/unittests/Support/VirtualFileSystemTest.cpp
index e9fd9671ea6ab..d12fd0ae2d503 100644
--- a/llvm/unittests/Support/VirtualFileSystemTest.cpp
+++ b/llvm/unittests/Support/VirtualFileSystemTest.cpp
@@ -1138,6 +1138,8 @@ TEST_F(InMemoryFileSystemTest, DuplicatedFile) {
ASSERT_FALSE(FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("a")));
ASSERT_TRUE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a")));
ASSERT_FALSE(FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("b")));
+ ASSERT_TRUE(FS.addFile("/b/c/d", 0, MemoryBuffer::getMemBuffer("a")));
+ ASSERT_FALSE(FS.addFile("/b/c", 0, MemoryBuffer::getMemBuffer("a")));
}
TEST_F(InMemoryFileSystemTest, DirectoryIteration) {
More information about the llvm-commits
mailing list