1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:27:35 +00:00

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

Same as Vector, ByteBuffer now also signals allocation failure by
returning an ENOMEM Error instead of a bool, allowing us to use the
TRY() and MUST() patterns.
This commit is contained in:
Andreas Kling 2021-11-10 14:33:44 +01:00
parent 88b6428c25
commit a15ed8743d
20 changed files with 59 additions and 70 deletions

View file

@ -460,7 +460,7 @@ static bool fill_getserv_buffers(const char* line, ssize_t read)
break;
}
auto alias = split_line[i].to_byte_buffer();
if (!alias.try_append("\0", sizeof(char)))
if (alias.try_append("\0", sizeof(char)).is_error())
return false;
__getserv_alias_list_buffer.append(move(alias));
}
@ -630,7 +630,7 @@ static bool fill_getproto_buffers(const char* line, ssize_t read)
if (split_line[i].starts_with('#'))
break;
auto alias = split_line[i].to_byte_buffer();
if (!alias.try_append("\0", sizeof(char)))
if (alias.try_append("\0", sizeof(char)).is_error())
return false;
__getproto_alias_list_buffer.append(move(alias));
}

View file

@ -39,7 +39,7 @@ ByteBuffer decode_pem(ReadonlyBytes data)
dbgln("Failed to decode PEM, likely bad Base64");
return {};
}
if (!decoded.try_append(b64decoded.value().data(), b64decoded.value().size())) {
if (decoded.try_append(b64decoded.value().data(), b64decoded.value().size()).is_error()) {
dbgln("Failed to decode PEM, likely OOM condition");
return {};
}

View file

@ -152,8 +152,8 @@ public:
for (size_t counter = 0; counter < length / HashFunction::DigestSize - 1; ++counter) {
hash_fn.update(seed);
hash_fn.update((u8*)&counter, 4);
if (!T.try_append(hash_fn.digest().data, HashFunction::DigestSize)) {
dbgln("EMSA_PSS: MGF1 digest failed, not enough space");
if (auto result = T.try_append(hash_fn.digest().data, HashFunction::DigestSize); result.is_error()) {
dbgln("EMSA_PSS: MGF1 digest failed: {}", result.error());
return;
}
}

View file

@ -143,8 +143,8 @@ ByteBuffer BMPWriter::dump(const RefPtr<Bitmap> bitmap, DibHeader dib_header)
}
}
if (!buffer.try_append(pixel_data.data(), pixel_data.size()))
dbgln("Failed to write {} bytes of pixel data to buffer", pixel_data.size());
if (auto result = buffer.try_append(pixel_data.data(), pixel_data.size()); result.is_error())
dbgln("Failed to write {} bytes of pixel data to buffer: {}", pixel_data.size(), result.error());
return buffer;
}

View file

@ -370,7 +370,7 @@ void Editor::insert(const u32 cp)
StringBuilder builder;
builder.append(Utf32View(&cp, 1));
auto str = builder.build();
if (!m_pending_chars.try_append(str.characters(), str.length()))
if (m_pending_chars.try_append(str.characters(), str.length()).is_error())
return;
readjust_anchored_styles(m_cursor, ModificationKind::Insertion);

View file

@ -273,8 +273,8 @@ bool Parser::initialize_hint_tables()
if (!buffer_result.has_value())
return false;
possible_merged_stream_buffer = buffer_result.release_value();
auto ok = possible_merged_stream_buffer.try_append(primary_hint_stream->bytes());
ok = ok && possible_merged_stream_buffer.try_append(overflow_hint_stream->bytes());
auto ok = !possible_merged_stream_buffer.try_append(primary_hint_stream->bytes()).is_error();
ok = ok && !possible_merged_stream_buffer.try_append(overflow_hint_stream->bytes()).is_error();
if (!ok)
return false;
hint_stream_bytes = possible_merged_stream_buffer.bytes();

View file

@ -75,7 +75,7 @@ bool Heap::write_block(u32 block, ByteBuffer& buffer)
VERIFY(buffer.size() <= BLOCKSIZE);
auto sz = buffer.size();
if (sz < BLOCKSIZE) {
if (!buffer.try_resize(BLOCKSIZE))
if (buffer.try_resize(BLOCKSIZE).is_error())
return false;
memset(buffer.offset_pointer((int)sz), 0, BLOCKSIZE - sz);
}

View file

@ -119,7 +119,7 @@ bool TLSv12::compute_master_secret_from_pre_master_secret(size_t length)
return false;
}
if (!m_context.master_key.try_resize(length)) {
if (m_context.master_key.try_resize(length).is_error()) {
dbgln("Couldn't allocate enough space for the master key :(");
return false;
}

View file

@ -56,8 +56,7 @@ void TLSv12::write_packet(ByteBuffer& packet)
if (m_context.tls_buffer.size() + packet.size() > 16 * KiB)
schedule_or_perform_flush(true);
auto ok = m_context.tls_buffer.try_append(packet.data(), packet.size());
if (!ok) {
if (m_context.tls_buffer.try_append(packet.data(), packet.size()).is_error()) {
// Toooooo bad, drop the record on the ground.
return;
}
@ -498,7 +497,7 @@ ssize_t TLSv12::handle_message(ReadonlyBytes buffer)
} else {
dbgln_if(TLS_DEBUG, "application data message of size {}", plain.size());
if (!m_context.application_buffer.try_append(plain.data(), plain.size())) {
if (m_context.application_buffer.try_append(plain.data(), plain.size()).is_error()) {
payload_res = (i8)Error::DecryptionFailed;
auto packet = build_alert(true, (u8)AlertDescription::DecryptionFailed);
write_packet(packet);

View file

@ -35,7 +35,7 @@ void TLSv12::consume(ReadonlyBytes record)
dbgln_if(TLS_DEBUG, "Consuming {} bytes", record.size());
if (!m_context.message_buffer.try_append(record)) {
if (m_context.message_buffer.try_append(record).is_error()) {
dbgln("Not enough space in message buffer, dropping the record");
return;
}

View file

@ -349,7 +349,7 @@ public:
return false;
}
auto previous_size = m_size;
if (!m_data.try_resize(new_size))
if (m_data.try_resize(new_size).is_error())
return false;
m_size = new_size;
// The spec requires that we zero out everything on grow

View file

@ -739,7 +739,7 @@ ParseResult<CustomSection> CustomSection::parse(InputStream& stream)
return name.error();
ByteBuffer data_buffer;
if (!data_buffer.try_resize(64))
if (data_buffer.try_resize(64).is_error())
return ParseError::OutOfMemory;
while (!stream.has_any_error() && !stream.unreliable_eof()) {
@ -747,7 +747,7 @@ ParseResult<CustomSection> CustomSection::parse(InputStream& stream)
auto size = stream.read({ buf, 16 });
if (size == 0)
break;
if (!data_buffer.try_append(buf, size))
if (data_buffer.try_append(buf, size).is_error())
return with_eof_check(stream, ParseError::HugeAllocationRequested);
}