1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 18:35:09 +00:00

Kernel: Make InodeWatcher::crate API OOM safe

This commit is contained in:
Brian Gianforcaro 2021-05-13 04:31:27 -07:00 committed by Andreas Kling
parent c8758d4faa
commit 0d50d3ed1e
3 changed files with 21 additions and 7 deletions

View file

@ -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));

View file

@ -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;

View file

@ -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();