From cbe1e057713869254ba7f18daf63e6341230e984 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Fri, 28 May 2021 05:41:31 -0700 Subject: [PATCH] Kernel: Move ProcFS API towards OOM safety --- Kernel/FileSystem/ProcFS.cpp | 10 ++++++---- Kernel/FileSystem/ProcFS.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index fc458844aa..434ba03257 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -234,9 +234,9 @@ struct ProcFSInodeData : public FileDescriptionData { RefPtr buffer; }; -NonnullRefPtr ProcFS::create() +RefPtr ProcFS::create() { - return adopt_ref(*new ProcFS); + return adopt_ref_if_nonnull(new ProcFS); } ProcFS::~ProcFS() @@ -1015,10 +1015,12 @@ RefPtr ProcFS::get_inode(InodeIdentifier inode_id) const // and if that fails we cannot return this instance anymore and just // create a new one. if (it->value->try_ref()) - return adopt_ref(*it->value); + return adopt_ref_if_nonnull(it->value); // We couldn't ref it, so just create a new one and replace the entry } - auto inode = adopt_ref(*new ProcFSInode(const_cast(*this), inode_id.index())); + auto inode = adopt_ref_if_nonnull(new ProcFSInode(const_cast(*this), inode_id.index())); + if (!inode) + return {}; auto result = m_inodes.set(inode_id.index().value(), inode.ptr()); VERIFY(result == ((it == m_inodes.end()) ? AK::HashSetResult::InsertedNewEntry : AK::HashSetResult::ReplacedExistingEntry)); return inode; diff --git a/Kernel/FileSystem/ProcFS.h b/Kernel/FileSystem/ProcFS.h index b54acbd0f7..4c34709811 100644 --- a/Kernel/FileSystem/ProcFS.h +++ b/Kernel/FileSystem/ProcFS.h @@ -24,7 +24,7 @@ class ProcFS final : public FS { public: virtual ~ProcFS() override; - static NonnullRefPtr create(); + static RefPtr create(); virtual bool initialize() override; virtual const char* class_name() const override;