1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 08:47:34 +00:00

Everywhere: Use OOM-safe ByteBuffer APIs where possible

If we can easily communicate failure, let's avoid asserting and report
failure instead.
This commit is contained in:
Ali Mohammad Pur 2021-09-06 03:28:46 +04:30 committed by Andreas Kling
parent 6606993432
commit 3a9f00c59b
22 changed files with 135 additions and 67 deletions

View file

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

View file

@ -37,7 +37,11 @@ void TLSv12::alert(AlertLevel level, AlertDescription code)
void TLSv12::write_packet(ByteBuffer& packet)
{
m_context.tls_buffer.append(packet.data(), packet.size());
auto ok = m_context.tls_buffer.try_append(packet.data(), packet.size());
if (!ok) {
// Toooooo bad, drop the record on the ground.
return;
}
if (m_context.connection_status > ConnectionStatus::Disconnected) {
if (!m_has_scheduled_write_flush) {
dbgln_if(TLS_DEBUG, "Scheduling write of {}", m_context.tls_buffer.size());
@ -451,7 +455,11 @@ ssize_t TLSv12::handle_message(ReadonlyBytes buffer)
} else {
dbgln_if(TLS_DEBUG, "application data message of size {}", plain.size());
m_context.application_buffer.append(plain.data(), plain.size());
if (!m_context.application_buffer.try_append(plain.data(), plain.size())) {
payload_res = (i8)Error::DecryptionFailed;
auto packet = build_alert(true, (u8)AlertDescription::DecryptionFailed);
write_packet(packet);
}
}
break;
case MessageType::Handshake:

View file

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