diff options
author | Brian Gianforcaro <bgianf@serenityos.org> | 2021-05-13 04:31:27 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-13 16:21:53 +0200 |
commit | 0d50d3ed1e143566f650abc48db626748975c1e6 (patch) | |
tree | 05c02749a21402926e23293b965aecc56c2d79af /Kernel | |
parent | c8758d4faafd3556da636dd973b09256b82bd5db (diff) | |
download | serenity-0d50d3ed1e143566f650abc48db626748975c1e6.zip |
Kernel: Make InodeWatcher::crate API OOM safe
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/FileSystem/InodeWatcher.cpp | 13 | ||||
-rw-r--r-- | Kernel/FileSystem/InodeWatcher.h | 9 | ||||
-rw-r--r-- | Kernel/Syscalls/inode_watcher.cpp | 6 |
3 files changed, 21 insertions, 7 deletions
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> InodeWatcher::create() +KResultOr<NonnullRefPtr<InodeWatcher>> 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<int> 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<WatchDescription> create(int wd, Inode& inode, unsigned event_mask) + static KResultOr<NonnullOwnPtr<WatchDescription>> 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<InodeWatcher> create(); + static KResultOr<NonnullRefPtr<InodeWatcher>> 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<int> 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(); |