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

Kernel: Make it possible for KBufferBuilder creation to fail

This patch adds KBufferBuilder::try_create() and treats it like anything
else that can fail. And so, failure to allocate the initial internal
buffer of the builder will now propagate an ENOMEM to the caller. :^)
This commit is contained in:
Andreas Kling 2021-09-07 15:54:23 +02:00
parent be613b9ef6
commit 300402cc14
9 changed files with 30 additions and 13 deletions

View file

@ -54,7 +54,7 @@ KResult SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description
if (!cached_data) {
cached_data = TRY(adopt_nonnull_own_or_enomem(new (nothrow) SysFSInodeData));
}
KBufferBuilder builder;
auto builder = TRY(KBufferBuilder::try_create());
TRY(const_cast<SysFSUSBDeviceInformation&>(*this).try_generate(builder));
auto& typed_cached_data = static_cast<SysFSInodeData&>(*cached_data);
typed_cached_data.buffer = builder.build();

View file

@ -281,7 +281,7 @@ KResult Coredump::write()
SpinlockLocker lock(m_process->address_space().get_lock());
ScopedAddressSpaceSwitcher switcher(m_process);
KBufferBuilder builder;
auto builder = TRY(KBufferBuilder::try_create());
TRY(create_notes_segment_data(builder));
TRY(write_elf_header());
TRY(write_program_headers(builder.bytes().size()));

View file

@ -47,7 +47,7 @@ void Inode::sync()
KResultOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(OpenFileDescription* description) const
{
KBufferBuilder builder;
auto builder = TRY(KBufferBuilder::try_create());
u8 buffer[4096];
off_t offset = 0;

View file

@ -323,7 +323,7 @@ StringView Plan9FS::Message::Decoder::read_data()
}
Plan9FS::Message::Message(Plan9FS& fs, Type type)
: m_builder()
: m_builder(KBufferBuilder::try_create().release_value()) // FIXME: Don't assume KBufferBuilder allocation success.
, m_tag(fs.allocate_tag())
, m_type(type)
, m_have_been_built(false)

View file

@ -480,7 +480,7 @@ KResultOr<size_t> ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t co
VERIFY(buffer.user_or_kernel_ptr());
if (!description) {
KBufferBuilder builder;
auto builder = TRY(KBufferBuilder::try_create());
auto process = Process::from_pid(associated_pid());
if (!process)
return KResult(ESRCH);
@ -579,7 +579,7 @@ KResult ProcFSProcessPropertyInode::refresh_data(OpenFileDescription& descriptio
if (!cached_data)
return ENOMEM;
}
KBufferBuilder builder;
auto builder = TRY(KBufferBuilder::try_create());
TRY(try_to_acquire_data(*process, builder));
return build_from_cached_data(builder, static_cast<ProcFSInodeData&>(*cached_data));
}

View file

@ -46,8 +46,14 @@ OwnPtr<KBuffer> KBufferBuilder::build()
return move(m_buffer);
}
KBufferBuilder::KBufferBuilder()
: m_buffer(KBuffer::try_create_with_size(4 * MiB, Memory::Region::Access::ReadWrite).release_value())
KResultOr<KBufferBuilder> KBufferBuilder::try_create()
{
auto buffer = TRY(KBuffer::try_create_with_size(4 * MiB, Memory::Region::Access::ReadWrite));
return KBufferBuilder { move(buffer) };
}
KBufferBuilder::KBufferBuilder(NonnullOwnPtr<KBuffer> buffer)
: m_buffer(move(buffer))
{
}

View file

@ -6,18 +6,22 @@
#pragma once
#include <AK/String.h>
#include <AK/StringView.h>
#include <Kernel/KBuffer.h>
#include <stdarg.h>
namespace Kernel {
class KBufferBuilder {
AK_MAKE_NONCOPYABLE(KBufferBuilder);
public:
using OutputType = KBuffer;
KBufferBuilder();
static KResultOr<KBufferBuilder> try_create();
KBufferBuilder(KBufferBuilder&&) = default;
KBufferBuilder& operator=(KBufferBuilder&&) = default;
~KBufferBuilder() = default;
KResult append(const StringView&);
@ -48,6 +52,8 @@ public:
}
private:
explicit KBufferBuilder(NonnullOwnPtr<KBuffer>);
bool check_expand(size_t);
u8* insertion_ptr()
{

View file

@ -575,7 +575,12 @@ bool Process::dump_perfcore()
}
auto& description = *description_or_error.value();
KBufferBuilder builder;
auto builder_or_error = KBufferBuilder::try_create();
if (builder_or_error.is_error()) {
dbgln("Failed to generate perfcore for pid {}: Could not allocate KBufferBuilder.", pid());
return false;
}
auto builder = builder_or_error.release_value();
if (!m_perf_event_buffer->to_json(builder)) {
dbgln("Failed to generate perfcore for pid {}: Could not serialize performance events to JSON.", pid().value());
return false;

View file

@ -144,7 +144,7 @@ KResult ProcFSGlobalInformation::refresh_data(OpenFileDescription& description)
if (!cached_data)
return ENOMEM;
}
KBufferBuilder builder;
auto builder = TRY(KBufferBuilder::try_create());
TRY(const_cast<ProcFSGlobalInformation&>(*this).try_generate(builder));
auto& typed_cached_data = static_cast<ProcFSInodeData&>(*cached_data);
typed_cached_data.buffer = builder.build();
@ -157,7 +157,7 @@ KResultOr<size_t> ProcFSExposedLink::read_bytes(off_t offset, size_t count, User
{
VERIFY(offset == 0);
MutexLocker locker(m_lock);
KBufferBuilder builder;
auto builder = TRY(KBufferBuilder::try_create());
if (!const_cast<ProcFSExposedLink&>(*this).acquire_link(builder))
return KResult(EFAULT);
auto blob = builder.build();