1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:58:12 +00:00

AK: Make Vector::try_* functions return ErrorOr<void>

Instead of signalling allocation failure with a bool return value
(false), we now use ErrorOr<void> and return ENOMEM as appropriate.
This allows us to use TRY() and MUST() with Vector. :^)
This commit is contained in:
Andreas Kling 2021-11-10 11:55:37 +01:00
parent cd49f30bea
commit 88b6428c25
16 changed files with 98 additions and 152 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2021, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
@ -8,6 +8,7 @@
#pragma once #pragma once
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/Error.h>
#include <AK/Find.h> #include <AK/Find.h>
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/Iterator.h> #include <AK/Iterator.h>
@ -204,49 +205,41 @@ public:
template<typename U = T> template<typename U = T>
void insert(size_t index, U&& value) requires(CanBePlacedInsideVector<U>) void insert(size_t index, U&& value) requires(CanBePlacedInsideVector<U>)
{ {
auto did_allocate = try_insert<U>(index, forward<U>(value)); MUST(try_insert<U>(index, forward<U>(value)));
VERIFY(did_allocate);
} }
template<typename TUnaryPredicate, typename U = T> template<typename TUnaryPredicate, typename U = T>
void insert_before_matching(U&& value, TUnaryPredicate predicate, size_t first_index = 0, size_t* inserted_index = nullptr) requires(CanBePlacedInsideVector<U>) void insert_before_matching(U&& value, TUnaryPredicate predicate, size_t first_index = 0, size_t* inserted_index = nullptr) requires(CanBePlacedInsideVector<U>)
{ {
auto did_allocate = try_insert_before_matching(forward<U>(value), predicate, first_index, inserted_index); MUST(try_insert_before_matching(forward<U>(value), predicate, first_index, inserted_index));
VERIFY(did_allocate);
} }
void extend(Vector&& other) void extend(Vector&& other)
{ {
auto did_allocate = try_extend(move(other)); MUST(try_extend(move(other)));
VERIFY(did_allocate);
} }
void extend(Vector const& other) void extend(Vector const& other)
{ {
auto did_allocate = try_extend(other); MUST(try_extend(other));
VERIFY(did_allocate);
} }
ALWAYS_INLINE void append(T&& value) ALWAYS_INLINE void append(T&& value)
{ {
bool did_allocate;
if constexpr (contains_reference) if constexpr (contains_reference)
did_allocate = try_append(value); MUST(try_append(value));
else else
did_allocate = try_append(move(value)); MUST(try_append(move(value)));
VERIFY(did_allocate);
} }
ALWAYS_INLINE void append(T const& value) requires(!contains_reference) ALWAYS_INLINE void append(T const& value) requires(!contains_reference)
{ {
auto did_allocate = try_append(T(value)); MUST(try_append(T(value)));
VERIFY(did_allocate);
} }
void append(StorageType const* values, size_t count) void append(StorageType const* values, size_t count)
{ {
auto did_allocate = try_append(values, count); MUST(try_append(values, count));
VERIFY(did_allocate);
} }
template<typename U = T> template<typename U = T>
@ -263,27 +256,23 @@ public:
template<class... Args> template<class... Args>
void empend(Args&&... args) requires(!contains_reference) void empend(Args&&... args) requires(!contains_reference)
{ {
auto did_allocate = try_empend(forward<Args>(args)...); MUST(try_empend(forward<Args>(args)...));
VERIFY(did_allocate);
} }
template<typename U = T> template<typename U = T>
void prepend(U&& value) requires(CanBePlacedInsideVector<U>) void prepend(U&& value) requires(CanBePlacedInsideVector<U>)
{ {
auto did_allocate = try_insert(0, forward<U>(value)); MUST(try_insert(0, forward<U>(value)));
VERIFY(did_allocate);
} }
void prepend(Vector&& other) void prepend(Vector&& other)
{ {
auto did_allocate = try_prepend(move(other)); MUST(try_prepend(move(other)));
VERIFY(did_allocate);
} }
void prepend(StorageType const* values, size_t count) void prepend(StorageType const* values, size_t count)
{ {
auto did_allocate = try_prepend(values, count); MUST(try_prepend(values, count));
VERIFY(did_allocate);
} }
// FIXME: What about assigning from a vector with lower inline capacity? // FIXME: What about assigning from a vector with lower inline capacity?
@ -451,14 +440,13 @@ public:
} }
template<typename U = T> template<typename U = T>
[[nodiscard]] bool try_insert(size_t index, U&& value) requires(CanBePlacedInsideVector<U>) ErrorOr<void> try_insert(size_t index, U&& value) requires(CanBePlacedInsideVector<U>)
{ {
if (index > size()) if (index > size())
return false; return Error::from_errno(EINVAL);
if (index == size()) if (index == size())
return try_append(forward<U>(value)); return try_append(forward<U>(value));
if (!try_grow_capacity(size() + 1)) TRY(try_grow_capacity(size() + 1));
return false;
++m_size; ++m_size;
if constexpr (Traits<StorageType>::is_trivial()) { if constexpr (Traits<StorageType>::is_trivial()) {
TypedTransfer<StorageType>::move(slot(index + 1), slot(index), m_size - index - 1); TypedTransfer<StorageType>::move(slot(index + 1), slot(index), m_size - index - 1);
@ -472,109 +460,101 @@ public:
new (slot(index)) StorageType(&value); new (slot(index)) StorageType(&value);
else else
new (slot(index)) StorageType(forward<U>(value)); new (slot(index)) StorageType(forward<U>(value));
return true; return {};
} }
template<typename TUnaryPredicate, typename U = T> template<typename TUnaryPredicate, typename U = T>
[[nodiscard]] bool try_insert_before_matching(U&& value, TUnaryPredicate predicate, size_t first_index = 0, size_t* inserted_index = nullptr) requires(CanBePlacedInsideVector<U>) ErrorOr<void> try_insert_before_matching(U&& value, TUnaryPredicate predicate, size_t first_index = 0, size_t* inserted_index = nullptr) requires(CanBePlacedInsideVector<U>)
{ {
for (size_t i = first_index; i < size(); ++i) { for (size_t i = first_index; i < size(); ++i) {
if (predicate(at(i))) { if (predicate(at(i))) {
if (!try_insert(i, forward<U>(value))) TRY(try_insert(i, forward<U>(value)));
return false;
if (inserted_index) if (inserted_index)
*inserted_index = i; *inserted_index = i;
return true; return {};
} }
} }
if (!try_append(forward<U>(value))) TRY(try_append(forward<U>(value)));
return false;
if (inserted_index) if (inserted_index)
*inserted_index = size() - 1; *inserted_index = size() - 1;
return true; return {};
} }
[[nodiscard]] bool try_extend(Vector&& other) ErrorOr<void> try_extend(Vector&& other)
{ {
if (is_empty()) { if (is_empty()) {
*this = move(other); *this = move(other);
return true; return {};
} }
auto other_size = other.size(); auto other_size = other.size();
Vector tmp = move(other); Vector tmp = move(other);
if (!try_grow_capacity(size() + other_size)) TRY(try_grow_capacity(size() + other_size));
return false;
TypedTransfer<StorageType>::move(data() + m_size, tmp.data(), other_size); TypedTransfer<StorageType>::move(data() + m_size, tmp.data(), other_size);
m_size += other_size; m_size += other_size;
return true; return {};
} }
[[nodiscard]] bool try_extend(Vector const& other) ErrorOr<void> try_extend(Vector const& other)
{ {
if (!try_grow_capacity(size() + other.size())) TRY(try_grow_capacity(size() + other.size()));
return false;
TypedTransfer<StorageType>::copy(data() + m_size, other.data(), other.size()); TypedTransfer<StorageType>::copy(data() + m_size, other.data(), other.size());
m_size += other.m_size; m_size += other.m_size;
return true; return {};
} }
[[nodiscard]] ALWAYS_INLINE bool try_append(T&& value) ErrorOr<void> try_append(T&& value)
{ {
if (!try_grow_capacity(size() + 1)) TRY(try_grow_capacity(size() + 1));
return false;
if constexpr (contains_reference) if constexpr (contains_reference)
new (slot(m_size)) StorageType(&value); new (slot(m_size)) StorageType(&value);
else else
new (slot(m_size)) StorageType(move(value)); new (slot(m_size)) StorageType(move(value));
++m_size; ++m_size;
return true; return {};
} }
[[nodiscard]] ALWAYS_INLINE bool try_append(T const& value) requires(!contains_reference) ErrorOr<void> try_append(T const& value) requires(!contains_reference)
{ {
return try_append(T(value)); return try_append(T(value));
} }
[[nodiscard]] bool try_append(StorageType const* values, size_t count) ErrorOr<void> try_append(StorageType const* values, size_t count)
{ {
if (!count) if (!count)
return true; return {};
if (!try_grow_capacity(size() + count)) TRY(try_grow_capacity(size() + count));
return false;
TypedTransfer<StorageType>::copy(slot(m_size), values, count); TypedTransfer<StorageType>::copy(slot(m_size), values, count);
m_size += count; m_size += count;
return true; return {};
} }
template<class... Args> template<class... Args>
[[nodiscard]] bool try_empend(Args&&... args) requires(!contains_reference) ErrorOr<void> try_empend(Args&&... args) requires(!contains_reference)
{ {
if (!try_grow_capacity(m_size + 1)) TRY(try_grow_capacity(m_size + 1));
return false;
new (slot(m_size)) StorageType { forward<Args>(args)... }; new (slot(m_size)) StorageType { forward<Args>(args)... };
++m_size; ++m_size;
return true; return {};
} }
template<typename U = T> template<typename U = T>
[[nodiscard]] bool try_prepend(U&& value) requires(CanBePlacedInsideVector<U>) ErrorOr<void> try_prepend(U&& value) requires(CanBePlacedInsideVector<U>)
{ {
return try_insert(0, forward<U>(value)); return try_insert(0, forward<U>(value));
} }
[[nodiscard]] bool try_prepend(Vector&& other) ErrorOr<void> try_prepend(Vector&& other)
{ {
if (other.is_empty()) if (other.is_empty())
return true; return {};
if (is_empty()) { if (is_empty()) {
*this = move(other); *this = move(other);
return true; return {};
} }
auto other_size = other.size(); auto other_size = other.size();
if (!try_grow_capacity(size() + other_size)) TRY(try_grow_capacity(size() + other_size));
return false;
for (size_t i = size() + other_size - 1; i >= other.size(); --i) { for (size_t i = size() + other_size - 1; i >= other.size(); --i) {
new (slot(i)) StorageType(move(at(i - other_size))); new (slot(i)) StorageType(move(at(i - other_size)));
@ -584,36 +564,35 @@ public:
Vector tmp = move(other); Vector tmp = move(other);
TypedTransfer<StorageType>::move(slot(0), tmp.data(), tmp.size()); TypedTransfer<StorageType>::move(slot(0), tmp.data(), tmp.size());
m_size += other_size; m_size += other_size;
return true; return {};
} }
[[nodiscard]] bool try_prepend(StorageType const* values, size_t count) ErrorOr<void> try_prepend(StorageType const* values, size_t count)
{ {
if (!count) if (!count)
return true; return {};
if (!try_grow_capacity(size() + count)) TRY(try_grow_capacity(size() + count));
return false;
TypedTransfer<StorageType>::move(slot(count), slot(0), m_size); TypedTransfer<StorageType>::move(slot(count), slot(0), m_size);
TypedTransfer<StorageType>::copy(slot(0), values, count); TypedTransfer<StorageType>::copy(slot(0), values, count);
m_size += count; m_size += count;
return true; return {};
} }
[[nodiscard]] bool try_grow_capacity(size_t needed_capacity) ErrorOr<void> try_grow_capacity(size_t needed_capacity)
{ {
if (m_capacity >= needed_capacity) if (m_capacity >= needed_capacity)
return true; return {};
return try_ensure_capacity(padded_capacity(needed_capacity)); return try_ensure_capacity(padded_capacity(needed_capacity));
} }
[[nodiscard]] bool try_ensure_capacity(size_t needed_capacity) ErrorOr<void> try_ensure_capacity(size_t needed_capacity)
{ {
if (m_capacity >= needed_capacity) if (m_capacity >= needed_capacity)
return true; return {};
size_t new_capacity = kmalloc_good_size(needed_capacity * sizeof(StorageType)) / sizeof(StorageType); size_t new_capacity = kmalloc_good_size(needed_capacity * sizeof(StorageType)) / sizeof(StorageType);
auto* new_buffer = static_cast<StorageType*>(kmalloc_array(new_capacity, sizeof(StorageType))); auto* new_buffer = static_cast<StorageType*>(kmalloc_array(new_capacity, sizeof(StorageType)));
if (new_buffer == nullptr) if (new_buffer == nullptr)
return false; return Error::from_errno(ENOMEM);
if constexpr (Traits<StorageType>::is_trivial()) { if constexpr (Traits<StorageType>::is_trivial()) {
TypedTransfer<StorageType>::copy(new_buffer, data(), m_size); TypedTransfer<StorageType>::copy(new_buffer, data(), m_size);
@ -627,40 +606,37 @@ public:
kfree_sized(m_outline_buffer, m_capacity * sizeof(StorageType)); kfree_sized(m_outline_buffer, m_capacity * sizeof(StorageType));
m_outline_buffer = new_buffer; m_outline_buffer = new_buffer;
m_capacity = new_capacity; m_capacity = new_capacity;
return true; return {};
} }
[[nodiscard]] bool try_resize(size_t new_size, bool keep_capacity = false) requires(!contains_reference) ErrorOr<void> try_resize(size_t new_size, bool keep_capacity = false) requires(!contains_reference)
{ {
if (new_size <= size()) { if (new_size <= size()) {
shrink(new_size, keep_capacity); shrink(new_size, keep_capacity);
return true; return {};
} }
if (!try_ensure_capacity(new_size)) TRY(try_ensure_capacity(new_size));
return false;
for (size_t i = size(); i < new_size; ++i) for (size_t i = size(); i < new_size; ++i)
new (slot(i)) StorageType {}; new (slot(i)) StorageType {};
m_size = new_size; m_size = new_size;
return true; return {};
} }
[[nodiscard]] bool try_resize_and_keep_capacity(size_t new_size) requires(!contains_reference) ErrorOr<void> try_resize_and_keep_capacity(size_t new_size) requires(!contains_reference)
{ {
return try_resize(new_size, true); return try_resize(new_size, true);
} }
void grow_capacity(size_t needed_capacity) void grow_capacity(size_t needed_capacity)
{ {
auto did_allocate = try_grow_capacity(needed_capacity); MUST(try_grow_capacity(needed_capacity));
VERIFY(did_allocate);
} }
void ensure_capacity(size_t needed_capacity) void ensure_capacity(size_t needed_capacity)
{ {
auto did_allocate = try_ensure_capacity(needed_capacity); MUST(try_ensure_capacity(needed_capacity));
VERIFY(did_allocate);
} }
void shrink(size_t new_size, bool keep_capacity = false) void shrink(size_t new_size, bool keep_capacity = false)
@ -684,14 +660,12 @@ public:
void resize(size_t new_size, bool keep_capacity = false) requires(!contains_reference) void resize(size_t new_size, bool keep_capacity = false) requires(!contains_reference)
{ {
auto did_allocate = try_resize(new_size, keep_capacity); MUST(try_resize(new_size, keep_capacity));
VERIFY(did_allocate);
} }
void resize_and_keep_capacity(size_t new_size) requires(!contains_reference) void resize_and_keep_capacity(size_t new_size) requires(!contains_reference)
{ {
auto did_allocate = try_resize_and_keep_capacity(new_size); MUST(try_resize_and_keep_capacity(new_size));
VERIFY(did_allocate);
} }
using ConstIterator = SimpleIterator<Vector const, T const>; using ConstIterator = SimpleIterator<Vector const, T const>;

View file

@ -917,8 +917,7 @@ ErrorOr<void> Ext2FSInode::resize(u64 new_size)
if (blocks_needed_after > blocks_needed_before) { if (blocks_needed_after > blocks_needed_before) {
auto blocks = TRY(fs().allocate_blocks(fs().group_index_from_inode(index()), blocks_needed_after - blocks_needed_before)); auto blocks = TRY(fs().allocate_blocks(fs().group_index_from_inode(index()), blocks_needed_after - blocks_needed_before));
if (!m_block_list.try_extend(move(blocks))) TRY(m_block_list.try_extend(move(blocks)));
return ENOMEM;
} else if (blocks_needed_after < blocks_needed_before) { } else if (blocks_needed_after < blocks_needed_before) {
if constexpr (EXT2_VERY_DEBUG) { if constexpr (EXT2_VERY_DEBUG) {
dbgln("Ext2FSInode[{}]::resize(): Shrinking inode, old block list is {} entries:", identifier(), m_block_list.size()); dbgln("Ext2FSInode[{}]::resize(): Shrinking inode, old block list is {} entries:", identifier(), m_block_list.size());
@ -1264,8 +1263,7 @@ auto Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count) ->
return Vector<BlockIndex> {}; return Vector<BlockIndex> {};
Vector<BlockIndex> blocks; Vector<BlockIndex> blocks;
if (!blocks.try_ensure_capacity(count)) TRY(blocks.try_ensure_capacity(count));
return ENOMEM;
MutexLocker locker(m_lock); MutexLocker locker(m_lock);
auto group_index = preferred_group_index; auto group_index = preferred_group_index;
@ -1454,8 +1452,7 @@ ErrorOr<Ext2FS::CachedBitmap*> Ext2FS::get_bitmap_block(BlockIndex bitmap_block_
auto buffer = UserOrKernelBuffer::for_kernel_buffer(block->data()); auto buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
TRY(read_block(bitmap_block_index, &buffer, block_size())); TRY(read_block(bitmap_block_index, &buffer, block_size()));
auto new_bitmap = TRY(adopt_nonnull_own_or_enomem(new (nothrow) CachedBitmap(bitmap_block_index, move(block)))); auto new_bitmap = TRY(adopt_nonnull_own_or_enomem(new (nothrow) CachedBitmap(bitmap_block_index, move(block))));
if (!m_cached_bitmaps.try_append(move(new_bitmap))) TRY(m_cached_bitmaps.try_append(move(new_bitmap)));
return ENOMEM;
return m_cached_bitmaps.last().ptr(); return m_cached_bitmaps.last().ptr();
} }

View file

@ -55,10 +55,7 @@ public:
if (has_flag(m_current_header->file_flags, ISO::FileFlags::Directory)) { if (has_flag(m_current_header->file_flags, ISO::FileFlags::Directory)) {
dbgln_if(ISO9660_VERY_DEBUG, "next(): Recursing"); dbgln_if(ISO9660_VERY_DEBUG, "next(): Recursing");
{ {
bool result = m_directory_stack.try_append(move(m_current_directory)); TRY(m_directory_stack.try_append(move(m_current_directory)));
if (!result) {
return ENOMEM;
}
} }
dbgln_if(ISO9660_VERY_DEBUG, "next(): Pushed into directory stack"); dbgln_if(ISO9660_VERY_DEBUG, "next(): Pushed into directory stack");

View file

@ -110,8 +110,7 @@ ErrorOr<void> AddressSpace::unmap_mmap_range(VirtualAddress addr, size_t size)
// Otherwise, split the regions and collect them for future mapping. // Otherwise, split the regions and collect them for future mapping.
auto split_regions = TRY(try_split_region_around_range(*region, range_to_unmap)); auto split_regions = TRY(try_split_region_around_range(*region, range_to_unmap));
if (new_regions.try_extend(split_regions)) TRY(new_regions.try_extend(split_regions));
return ENOMEM;
} }
// Give back any unwanted VM to the range allocator. // Give back any unwanted VM to the range allocator.

View file

@ -487,8 +487,7 @@ ErrorOr<void> LocalSocket::sendfd(OpenFileDescription const& socket_description,
// FIXME: Figure out how we should limit this properly. // FIXME: Figure out how we should limit this properly.
if (queue.size() > 128) if (queue.size() > 128)
return set_so_error(EBUSY); return set_so_error(EBUSY);
if (!queue.try_append(move(passing_description))) SOCKET_TRY(queue.try_append(move(passing_description)));
return set_so_error(ENOMEM);
return {}; return {};
} }

View file

@ -71,8 +71,7 @@ ErrorOr<void> Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
MutexLocker locker(mutex()); MutexLocker locker(mutex());
if (m_pending.size() >= m_backlog) if (m_pending.size() >= m_backlog)
return set_so_error(ECONNREFUSED); return set_so_error(ECONNREFUSED);
if (!m_pending.try_append(peer)) SOCKET_TRY(m_pending.try_append(peer));
return set_so_error(ENOMEM);
evaluate_block_conditions(); evaluate_block_conditions();
return {}; return {};
} }

View file

@ -144,18 +144,15 @@ ErrorOr<NonnullRefPtr<Process>> Process::try_create_user_process(RefPtr<Thread>&
auto parts = path.split_view('/'); auto parts = path.split_view('/');
if (arguments.is_empty()) { if (arguments.is_empty()) {
auto last_part = TRY(KString::try_create(parts.last())); auto last_part = TRY(KString::try_create(parts.last()));
if (!arguments.try_append(move(last_part))) TRY(arguments.try_append(move(last_part)));
return ENOMEM;
} }
auto path_string = TRY(KString::try_create(path)); auto path_string = TRY(KString::try_create(path));
auto name = TRY(KString::try_create(parts.last())); auto name = TRY(KString::try_create(parts.last()));
auto process = TRY(Process::try_create(first_thread, move(name), uid, gid, ProcessID(0), false, VirtualFileSystem::the().root_custody(), nullptr, tty)); auto process = TRY(Process::try_create(first_thread, move(name), uid, gid, ProcessID(0), false, VirtualFileSystem::the().root_custody(), nullptr, tty));
if (!process->m_fds.try_resize(process->m_fds.max_open())) { TRY(process->m_fds.try_resize(process->m_fds.max_open()));
first_thread = nullptr;
return ENOMEM;
}
auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : DeviceManagement::the().null_device(); auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : DeviceManagement::the().null_device();
auto description = TRY(device_to_use_as_tty.open(O_RDWR)); auto description = TRY(device_to_use_as_tty.open(O_RDWR));
auto setup_description = [&process, &description](int fd) { auto setup_description = [&process, &description](int fd) {

View file

@ -641,8 +641,7 @@ public:
ErrorOr<void> try_clone(const Kernel::Process::OpenFileDescriptions& other) ErrorOr<void> try_clone(const Kernel::Process::OpenFileDescriptions& other)
{ {
SpinlockLocker lock_other(other.m_fds_lock); SpinlockLocker lock_other(other.m_fds_lock);
if (!try_resize(other.m_fds_metadatas.size())) TRY(try_resize(other.m_fds_metadatas.size()));
return ENOMEM;
for (size_t i = 0; i < other.m_fds_metadatas.size(); ++i) { for (size_t i = 0; i < other.m_fds_metadatas.size(); ++i) {
m_fds_metadatas[i] = other.m_fds_metadatas[i]; m_fds_metadatas[i] = other.m_fds_metadatas[i];
@ -662,7 +661,7 @@ public:
ErrorOr<ScopedDescriptionAllocation> allocate(int first_candidate_fd = 0); ErrorOr<ScopedDescriptionAllocation> allocate(int first_candidate_fd = 0);
size_t open_count() const; size_t open_count() const;
bool try_resize(size_t size) { return m_fds_metadatas.try_resize(size); } ErrorOr<void> try_resize(size_t size) { return m_fds_metadatas.try_resize(size); }
size_t max_open() const size_t max_open() const
{ {

View file

@ -100,15 +100,13 @@ static ErrorOr<FlatPtr> make_userspace_context_for_main_thread([[maybe_unused]]
Vector<FlatPtr> argv_entries; Vector<FlatPtr> argv_entries;
for (auto& argument : arguments) { for (auto& argument : arguments) {
push_string_on_new_stack(argument.view()); push_string_on_new_stack(argument.view());
if (!argv_entries.try_append(new_sp)) TRY(argv_entries.try_append(new_sp));
return ENOMEM;
} }
Vector<FlatPtr> env_entries; Vector<FlatPtr> env_entries;
for (auto& variable : environment) { for (auto& variable : environment) {
push_string_on_new_stack(variable.view()); push_string_on_new_stack(variable.view());
if (!env_entries.try_append(new_sp)) TRY(env_entries.try_append(new_sp));
return ENOMEM;
} }
for (auto& value : auxiliary_values) { for (auto& value : auxiliary_values) {
@ -810,8 +808,7 @@ ErrorOr<void> Process::exec(NonnullOwnPtr<KString> path, NonnullOwnPtrVector<KSt
auto shebang_words = shebang_result.release_value(); auto shebang_words = shebang_result.release_value();
auto shebang_path = TRY(shebang_words.first().try_clone()); auto shebang_path = TRY(shebang_words.first().try_clone());
arguments.ptr_at(0) = move(path); arguments.ptr_at(0) = move(path);
if (!arguments.try_prepend(move(shebang_words))) TRY(arguments.try_prepend(move(shebang_words)));
return ENOMEM;
return exec(move(shebang_path), move(arguments), move(environment), ++recursion_depth); return exec(move(shebang_path), move(arguments), move(environment), ++recursion_depth);
} }
@ -881,13 +878,11 @@ ErrorOr<FlatPtr> Process::sys$execve(Userspace<const Syscall::SC_execve_params*>
if (size.has_overflow()) if (size.has_overflow())
return EOVERFLOW; return EOVERFLOW;
Vector<Syscall::StringArgument, 32> strings; Vector<Syscall::StringArgument, 32> strings;
if (!strings.try_resize(list.length)) TRY(strings.try_resize(list.length));
return ENOMEM;
TRY(copy_from_user(strings.data(), list.strings, size.value())); TRY(copy_from_user(strings.data(), list.strings, size.value()));
for (size_t i = 0; i < list.length; ++i) { for (size_t i = 0; i < list.length; ++i) {
auto string = TRY(try_copy_kstring_from_user(strings[i])); auto string = TRY(try_copy_kstring_from_user(strings[i]));
if (!output.try_append(move(string))) TRY(output.try_append(move(string)));
return ENOMEM;
} }
return {}; return {};
}; };

View file

@ -27,8 +27,8 @@ ErrorOr<FlatPtr> Process::sys$purge(int mode)
if (vmobject.is_anonymous()) { if (vmobject.is_anonymous()) {
// In the event that the append fails, only attempt to continue // In the event that the append fails, only attempt to continue
// the purge if we have already appended something successfully. // the purge if we have already appended something successfully.
if (!vmobjects.try_append(static_cast<Memory::AnonymousVMObject&>(vmobject)) && vmobjects.is_empty()) { if (auto append_result = vmobjects.try_append(static_cast<Memory::AnonymousVMObject&>(vmobject)); append_result.is_error() && vmobjects.is_empty()) {
result = ENOMEM; result = append_result.release_error();
return IterationDecision::Break; return IterationDecision::Break;
} }
} }
@ -50,8 +50,8 @@ ErrorOr<FlatPtr> Process::sys$purge(int mode)
if (vmobject.is_inode()) { if (vmobject.is_inode()) {
// In the event that the append fails, only attempt to continue // In the event that the append fails, only attempt to continue
// the purge if we have already appended something successfully. // the purge if we have already appended something successfully.
if (!vmobjects.try_append(static_cast<Memory::InodeVMObject&>(vmobject)) && vmobjects.is_empty()) { if (auto append_result = vmobjects.try_append(static_cast<Memory::InodeVMObject&>(vmobject)); append_result.is_error() && vmobjects.is_empty()) {
result = ENOMEM; result = append_result.release_error();
return IterationDecision::Break; return IterationDecision::Break;
} }
} }

View file

@ -50,8 +50,7 @@ ErrorOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov,
u64 total_length = 0; u64 total_length = 0;
Vector<iovec, 32> vecs; Vector<iovec, 32> vecs;
if (!vecs.try_resize(iov_count)) TRY(vecs.try_resize(iov_count));
return ENOMEM;
TRY(copy_n_from_user(vecs.data(), iov, iov_count)); TRY(copy_n_from_user(vecs.data(), iov, iov_count));
for (auto& vec : vecs) { for (auto& vec : vecs) {
total_length += vec.iov_len; total_length += vec.iov_len;

View file

@ -72,10 +72,8 @@ ErrorOr<FlatPtr> Process::sys$select(Userspace<const Syscall::SC_select_params*>
continue; continue;
auto description = TRY(fds().open_file_description(fd)); auto description = TRY(fds().open_file_description(fd));
if (!fds_info.try_append({ move(description), block_flags })) TRY(fds_info.try_append({ move(description), block_flags }));
return ENOMEM; TRY(selected_fds.try_append(fd));
if (!selected_fds.try_append(fd))
return ENOMEM;
} }
if constexpr (IO_DEBUG || POLL_SELECT_DEBUG) if constexpr (IO_DEBUG || POLL_SELECT_DEBUG)
@ -146,8 +144,7 @@ ErrorOr<FlatPtr> Process::sys$poll(Userspace<const Syscall::SC_poll_params*> use
nfds_checked *= params.nfds; nfds_checked *= params.nfds;
if (nfds_checked.has_overflow()) if (nfds_checked.has_overflow())
return EFAULT; return EFAULT;
if (!fds_copy.try_resize(params.nfds)) TRY(fds_copy.try_resize(params.nfds));
return ENOMEM;
TRY(copy_from_user(fds_copy.data(), &params.fds[0], nfds_checked.value())); TRY(copy_from_user(fds_copy.data(), &params.fds[0], nfds_checked.value()));
} }
@ -162,8 +159,7 @@ ErrorOr<FlatPtr> Process::sys$poll(Userspace<const Syscall::SC_poll_params*> use
block_flags |= BlockFlags::Write; block_flags |= BlockFlags::Write;
if (pfd.events & POLLPRI) if (pfd.events & POLLPRI)
block_flags |= BlockFlags::ReadPriority; block_flags |= BlockFlags::ReadPriority;
if (!fds_info.try_append({ move(description), block_flags })) TRY(fds_info.try_append({ move(description), block_flags }));
return ENOMEM;
} }
auto current_thread = Thread::current(); auto current_thread = Thread::current();

View file

@ -169,8 +169,7 @@ ErrorOr<FlatPtr> Process::sys$setgroups(size_t count, Userspace<const gid_t*> us
} }
Vector<gid_t> new_extra_gids; Vector<gid_t> new_extra_gids;
if (!new_extra_gids.try_resize(count)) TRY(new_extra_gids.try_resize(count));
return ENOMEM;
TRY(copy_n_from_user(new_extra_gids.data(), user_gids, count)); TRY(copy_n_from_user(new_extra_gids.data(), user_gids, count));
HashTable<gid_t> unique_extra_gids; HashTable<gid_t> unique_extra_gids;
@ -180,8 +179,7 @@ ErrorOr<FlatPtr> Process::sys$setgroups(size_t count, Userspace<const gid_t*> us
} }
ProtectedDataMutationScope scope { *this }; ProtectedDataMutationScope scope { *this };
if (!m_protected_values.extra_gids.try_resize(unique_extra_gids.size())) TRY(m_protected_values.extra_gids.try_resize(unique_extra_gids.size()));
return ENOMEM;
size_t i = 0; size_t i = 0;
for (auto& extra_gid : unique_extra_gids) { for (auto& extra_gid : unique_extra_gids) {
if (extra_gid == gid()) if (extra_gid == gid())

View file

@ -168,8 +168,7 @@ ErrorOr<FlatPtr> Process::sys$sendmsg(int sockfd, Userspace<const struct msghdr*
if (msg.msg_iovlen != 1) if (msg.msg_iovlen != 1)
return ENOTSUP; // FIXME: Support this :) return ENOTSUP; // FIXME: Support this :)
Vector<iovec, 1> iovs; Vector<iovec, 1> iovs;
if (!iovs.try_resize(msg.msg_iovlen)) TRY(iovs.try_resize(msg.msg_iovlen));
return ENOMEM;
TRY(copy_n_from_user(iovs.data(), msg.msg_iov, msg.msg_iovlen)); TRY(copy_n_from_user(iovs.data(), msg.msg_iov, msg.msg_iovlen));
if (iovs[0].iov_len > NumericLimits<ssize_t>::max()) if (iovs[0].iov_len > NumericLimits<ssize_t>::max())
return EINVAL; return EINVAL;
@ -201,8 +200,7 @@ ErrorOr<FlatPtr> Process::sys$recvmsg(int sockfd, Userspace<struct msghdr*> user
if (msg.msg_iovlen != 1) if (msg.msg_iovlen != 1)
return ENOTSUP; // FIXME: Support this :) return ENOTSUP; // FIXME: Support this :)
Vector<iovec, 1> iovs; Vector<iovec, 1> iovs;
if (!iovs.try_resize(msg.msg_iovlen)) TRY(iovs.try_resize(msg.msg_iovlen));
return ENOMEM;
TRY(copy_n_from_user(iovs.data(), msg.msg_iov, msg.msg_iovlen)); TRY(copy_n_from_user(iovs.data(), msg.msg_iov, msg.msg_iovlen));
Userspace<sockaddr*> user_addr((FlatPtr)msg.msg_name); Userspace<sockaddr*> user_addr((FlatPtr)msg.msg_name);

View file

@ -24,8 +24,7 @@ ErrorOr<FlatPtr> Process::sys$writev(int fd, Userspace<const struct iovec*> iov,
u64 total_length = 0; u64 total_length = 0;
Vector<iovec, 32> vecs; Vector<iovec, 32> vecs;
if (!vecs.try_resize(iov_count)) TRY(vecs.try_resize(iov_count));
return ENOMEM;
TRY(copy_n_from_user(vecs.data(), iov, iov_count)); TRY(copy_n_from_user(vecs.data(), iov, iov_count));
for (auto& vec : vecs) { for (auto& vec : vecs) {
total_length += vec.iov_len; total_length += vec.iov_len;

View file

@ -311,7 +311,7 @@ public:
return false; return false;
} }
auto previous_size = m_elements.size(); auto previous_size = m_elements.size();
if (!m_elements.try_resize(new_size)) if (m_elements.try_resize(new_size).is_error())
return false; return false;
for (size_t i = previous_size; i < m_elements.size(); ++i) for (size_t i = previous_size; i < m_elements.size(); ++i)
m_elements[i] = fill_value; m_elements[i] = fill_value;