diff --git a/Kernel/FileSystem/InodeWatcher.cpp b/Kernel/FileSystem/InodeWatcher.cpp index ea66d7cc3a..fb7c36739a 100644 --- a/Kernel/FileSystem/InodeWatcher.cpp +++ b/Kernel/FileSystem/InodeWatcher.cpp @@ -12,9 +12,12 @@ namespace Kernel { -NonnullRefPtr InodeWatcher::create() +KResultOr> InodeWatcher::create() { - return adopt_ref(*new InodeWatcher); + auto watcher = adopt_ref_if_nonnull(new InodeWatcher); + if (watcher) + return watcher.release_nonnull(); + return ENOMEM; } InodeWatcher::~InodeWatcher() @@ -120,7 +123,11 @@ KResultOr InodeWatcher::register_inode(Inode& inode, unsigned event_mask) m_wd_counter = 1; } while (m_wd_to_watches.find(wd) != m_wd_to_watches.end()); - auto description = WatchDescription::create(wd, inode, event_mask); + auto description_or_error = WatchDescription::create(wd, inode, event_mask); + if (description_or_error.is_error()) + return description_or_error.error(); + + auto description = description_or_error.release_value(); m_inode_to_watches.set(inode.identifier(), description.ptr()); m_wd_to_watches.set(wd, move(description)); diff --git a/Kernel/FileSystem/InodeWatcher.h b/Kernel/FileSystem/InodeWatcher.h index f429e17ab4..7a7c68d2dd 100644 --- a/Kernel/FileSystem/InodeWatcher.h +++ b/Kernel/FileSystem/InodeWatcher.h @@ -25,9 +25,12 @@ struct WatchDescription { Inode& inode; unsigned event_mask; - static NonnullOwnPtr create(int wd, Inode& inode, unsigned event_mask) + static KResultOr> create(int wd, Inode& inode, unsigned event_mask) { - return adopt_own(*new WatchDescription(wd, inode, event_mask)); + auto description = adopt_own_if_nonnull(new WatchDescription(wd, inode, event_mask)); + if (description) + return description.release_nonnull(); + return ENOMEM; } private: @@ -41,7 +44,7 @@ private: class InodeWatcher final : public File { public: - static NonnullRefPtr create(); + static KResultOr> create(); virtual ~InodeWatcher() override; virtual bool can_read(const FileDescription&, size_t) const override; diff --git a/Kernel/Syscalls/inode_watcher.cpp b/Kernel/Syscalls/inode_watcher.cpp index fc9e878acb..537301974f 100644 --- a/Kernel/Syscalls/inode_watcher.cpp +++ b/Kernel/Syscalls/inode_watcher.cpp @@ -21,7 +21,11 @@ KResultOr Process::sys$create_inode_watcher(u32 flags) if (fd < 0) return fd; - auto description_or_error = FileDescription::create(*InodeWatcher::create()); + auto watcher_or_error = InodeWatcher::create(); + if (watcher_or_error.is_error()) + return watcher_or_error.error(); + + auto description_or_error = FileDescription::create(*watcher_or_error.value()); if (description_or_error.is_error()) return description_or_error.error();