浏览代码

Kernel/TmpFS: Prevent TmpFS::add_child() from adding duplicate children

If asked to add an already existing name to a directory inode, fail with
EEXIST, consistent with other filesystems.
Andreas Kling 3 年之前
父节点
当前提交
db4388f21b
共有 1 个文件被更改,包括 7 次插入1 次删除
  1. 7 1
      Kernel/FileSystem/TmpFS.cpp

+ 7 - 1
Kernel/FileSystem/TmpFS.cpp

@@ -271,13 +271,19 @@ ErrorOr<void> TmpFSInode::add_child(Inode& child, StringView name, mode_t)
     if (name.length() > NAME_MAX)
         return ENAMETOOLONG;
 
+    MutexLocker locker(m_inode_lock);
+    for (auto const& existing_child : m_children) {
+        if (existing_child.name->view() == name)
+            return EEXIST;
+    }
+
     auto name_kstring = TRY(KString::try_create(name));
     // Balanced by `delete` in remove_child()
+
     auto* child_entry = new (nothrow) Child { move(name_kstring), static_cast<TmpFSInode&>(child) };
     if (!child_entry)
         return ENOMEM;
 
-    MutexLocker locker(m_inode_lock);
     m_children.append(*child_entry);
     did_add_child(child.identifier(), name);
     return {};