Skip to content

[clang] Fix handling of adding a file with the same name as an existing dir to VFS #94461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 6, 2024

Conversation

jensmassberg
Copy link
Contributor

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.

The problem is that the file name is recognised as existing path 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 that rejects adding a path if a file of the same name already exists.

@jensmassberg jensmassberg added the clang Clang issues not falling into any other category label Jun 5, 2024
@jensmassberg jensmassberg requested a review from kadircet June 5, 2024 11:42
@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2024

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-clang

Author: None (jensmassberg)

Changes

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.

The problem is that the file name is recognised as existing path 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 that rejects adding a path if a file of the same name already exists.


Full diff: https://github.com/llvm/llvm-project/pull/94461.diff

2 Files Affected:

  • (modified) llvm/lib/Support/VirtualFileSystem.cpp (+4)
  • (modified) llvm/unittests/Support/VirtualFileSystemTest.cpp (+2)
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) {

@jensmassberg
Copy link
Contributor Author

Thanks for the comments! I have refactored the code as suggested and added a new test case.

@jensmassberg jensmassberg requested a review from kadircet June 6, 2024 09:39
Copy link
Member

@kadircet kadircet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, lgtm!

@jensmassberg jensmassberg merged commit 09f19c7 into llvm:main Jun 6, 2024
7 checks passed
@jensmassberg jensmassberg deleted the 2024-06-05_vfs_bug branch June 6, 2024 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category llvm:support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants