Kernel: Harden DevFS Vector usage against OOM.

The dance here is not complicated, but it is something that should
be taken note of. Since we append to both lists, we don't want to
orphan the new Inode in the m_links/m_subfolders Vector in the event
that the append to m_parent_fs.m_nodes fails.
This commit is contained in:
Brian Gianforcaro 2021-04-30 03:14:29 -07:00 committed by Linus Groh
parent a678851b41
commit ee84b8a845
Notes: sideshowbarker 2024-07-18 18:50:09 +09:00

View File

@ -275,6 +275,10 @@ KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::create_child(const Stri
if (name != "pts")
return EROFS;
auto new_directory_inode = adopt_ref(*new DevFSPtsDirectoryInode(m_parent_fs));
if (!m_subfolders.try_ensure_capacity(m_subfolders.size() + 1))
return ENOMEM;
if (!m_parent_fs.m_nodes.try_ensure_capacity(m_parent_fs.m_nodes.size() + 1))
return ENOMEM;
m_subfolders.append(new_directory_inode);
m_parent_fs.m_nodes.append(new_directory_inode);
return KResult(KSuccess);
@ -285,6 +289,10 @@ KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::create_child(const Stri
return EEXIST;
}
auto new_link_inode = adopt_ref(*new DevFSLinkInode(m_parent_fs, name));
if (!m_links.try_ensure_capacity(m_links.size() + 1))
return ENOMEM;
if (!m_parent_fs.m_nodes.try_ensure_capacity(m_parent_fs.m_nodes.size() + 1))
return ENOMEM;
m_links.append(new_link_inode);
m_parent_fs.m_nodes.append(new_link_inode);
return new_link_inode;