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

Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe

This commit is contained in:
Ali Mohammad Pur 2021-09-06 03:29:52 +04:30 committed by Andreas Kling
parent 3a9f00c59b
commit 97e97bccab
105 changed files with 629 additions and 290 deletions

View file

@ -231,13 +231,17 @@ protected:
// Sometimes we might receive a partial message. That's okay, just stash away
// the unprocessed bytes and we'll prepend them to the next incoming message
// in the next run of this function.
auto remaining_bytes = ByteBuffer::copy(bytes.data() + index, bytes.size() - index);
auto remaining_bytes_result = ByteBuffer::copy(bytes.span().slice(index));
if (!remaining_bytes_result.has_value()) {
dbgln("{}::drain_messages_from_peer: Failed to allocate buffer", *this);
return false;
}
if (!m_unprocessed_bytes.is_empty()) {
dbgln("{}::drain_messages_from_peer: Already have unprocessed bytes", *this);
shutdown();
return false;
}
m_unprocessed_bytes = remaining_bytes;
m_unprocessed_bytes = remaining_bytes_result.release_value();
}
if (!m_unprocessed_messages.is_empty()) {

View file

@ -110,10 +110,14 @@ bool Decoder::decode(ByteBuffer& value)
return true;
}
if (length == 0) {
value = ByteBuffer::create_uninitialized(0);
value = {};
return true;
}
value = ByteBuffer::create_uninitialized(length);
if (auto buffer_result = ByteBuffer::create_uninitialized(length); buffer_result.has_value())
value = buffer_result.release_value();
else
return false;
m_stream >> value.bytes();
return !m_stream.handle_any_error();
}