mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:27:46 +00:00
Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe
This commit is contained in:
parent
3a9f00c59b
commit
97e97bccab
105 changed files with 629 additions and 290 deletions
|
@ -87,7 +87,8 @@ ByteBuffer decode_base64(const StringView& input)
|
|||
output.append(out2);
|
||||
}
|
||||
|
||||
return ByteBuffer::copy(output.data(), output.size());
|
||||
// FIXME: Handle OOM failure.
|
||||
return ByteBuffer::copy(output).release_value();
|
||||
}
|
||||
|
||||
String encode_base64(ReadonlyBytes input)
|
||||
|
|
|
@ -62,31 +62,35 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] static ByteBuffer create_uninitialized(size_t size)
|
||||
[[nodiscard]] static Optional<ByteBuffer> create_uninitialized(size_t size)
|
||||
{
|
||||
auto buffer = ByteBuffer();
|
||||
auto ok = buffer.try_resize(size);
|
||||
VERIFY(ok);
|
||||
return buffer;
|
||||
if (!buffer.try_resize(size))
|
||||
return {};
|
||||
return { move(buffer) };
|
||||
}
|
||||
|
||||
[[nodiscard]] static ByteBuffer create_zeroed(size_t size)
|
||||
[[nodiscard]] static Optional<ByteBuffer> create_zeroed(size_t size)
|
||||
{
|
||||
auto buffer = create_uninitialized(size);
|
||||
auto buffer_result = create_uninitialized(size);
|
||||
if (!buffer_result.has_value())
|
||||
return {};
|
||||
|
||||
auto& buffer = buffer_result.value();
|
||||
buffer.zero_fill();
|
||||
VERIFY(size == 0 || (buffer[0] == 0 && buffer[size - 1] == 0));
|
||||
return buffer;
|
||||
return buffer_result;
|
||||
}
|
||||
|
||||
[[nodiscard]] static ByteBuffer copy(void const* data, size_t size)
|
||||
[[nodiscard]] static Optional<ByteBuffer> copy(void const* data, size_t size)
|
||||
{
|
||||
auto buffer = create_uninitialized(size);
|
||||
if (size != 0)
|
||||
__builtin_memcpy(buffer.data(), data, size);
|
||||
if (buffer.has_value() && size != 0)
|
||||
__builtin_memcpy(buffer->data(), data, size);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
[[nodiscard]] static ByteBuffer copy(ReadonlyBytes bytes)
|
||||
[[nodiscard]] static Optional<ByteBuffer> copy(ReadonlyBytes bytes)
|
||||
{
|
||||
return copy(bytes.data(), bytes.size());
|
||||
}
|
||||
|
@ -133,12 +137,13 @@ public:
|
|||
[[nodiscard]] void* end_pointer() { return data() + m_size; }
|
||||
[[nodiscard]] void const* end_pointer() const { return data() + m_size; }
|
||||
|
||||
// FIXME: Make this function handle failures too.
|
||||
[[nodiscard]] ByteBuffer slice(size_t offset, size_t size) const
|
||||
{
|
||||
// I cannot hand you a slice I don't have
|
||||
VERIFY(offset + size <= this->size());
|
||||
|
||||
return copy(offset_pointer(offset), size);
|
||||
return copy(offset_pointer(offset), size).release_value();
|
||||
}
|
||||
|
||||
void clear()
|
||||
|
@ -237,8 +242,10 @@ private:
|
|||
if (!other.m_inline) {
|
||||
m_outline_buffer = other.m_outline_buffer;
|
||||
m_outline_capacity = other.m_outline_capacity;
|
||||
} else
|
||||
} else {
|
||||
VERIFY(other.m_size <= inline_capacity);
|
||||
__builtin_memcpy(m_inline_buffer, other.m_inline_buffer, other.m_size);
|
||||
}
|
||||
other.m_size = 0;
|
||||
other.m_inline = true;
|
||||
}
|
||||
|
|
|
@ -20,7 +20,11 @@ Optional<ByteBuffer> decode_hex(const StringView& input)
|
|||
if ((input.length() % 2) != 0)
|
||||
return {};
|
||||
|
||||
auto output = ByteBuffer::create_zeroed(input.length() / 2);
|
||||
auto output_result = ByteBuffer::create_zeroed(input.length() / 2);
|
||||
if (!output_result.has_value())
|
||||
return {};
|
||||
|
||||
auto& output = output_result.value();
|
||||
|
||||
for (size_t i = 0; i < input.length() / 2; ++i) {
|
||||
const auto c1 = decode_hex_digit(input[i * 2]);
|
||||
|
@ -34,7 +38,7 @@ Optional<ByteBuffer> decode_hex(const StringView& input)
|
|||
output[i] = (c1 << 4) + c2;
|
||||
}
|
||||
|
||||
return output;
|
||||
return output_result;
|
||||
}
|
||||
|
||||
String encode_hex(const ReadonlyBytes input)
|
||||
|
|
|
@ -224,7 +224,7 @@ public:
|
|||
size_t nwritten = 0;
|
||||
while (bytes.size() - nwritten > 0) {
|
||||
if ((m_write_offset + nwritten) % chunk_size == 0)
|
||||
m_chunks.append(ByteBuffer::create_uninitialized(chunk_size));
|
||||
m_chunks.append(ByteBuffer::create_uninitialized(chunk_size).release_value()); // FIXME: Handle possible OOM situation.
|
||||
|
||||
nwritten += bytes.slice(nwritten).copy_trimmed_to(m_chunks.last().bytes().slice((m_write_offset + nwritten) % chunk_size));
|
||||
}
|
||||
|
@ -241,7 +241,8 @@ public:
|
|||
|
||||
ByteBuffer copy_into_contiguous_buffer() const
|
||||
{
|
||||
auto buffer = ByteBuffer::create_uninitialized(size());
|
||||
// FIXME: Handle possible OOM situation.
|
||||
auto buffer = ByteBuffer::create_uninitialized(size()).release_value();
|
||||
|
||||
const auto nread = read_without_consuming(buffer);
|
||||
VERIFY(nread == buffer.size());
|
||||
|
|
|
@ -176,7 +176,8 @@ ByteBuffer String::to_byte_buffer() const
|
|||
{
|
||||
if (!m_impl)
|
||||
return {};
|
||||
return ByteBuffer::copy(reinterpret_cast<const u8*>(characters()), length());
|
||||
// FIXME: Handle OOM failure.
|
||||
return ByteBuffer::copy(bytes()).release_value();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
|
@ -69,7 +69,8 @@ void StringBuilder::appendvf(char const* fmt, va_list ap)
|
|||
|
||||
ByteBuffer StringBuilder::to_byte_buffer() const
|
||||
{
|
||||
return ByteBuffer::copy(data(), length());
|
||||
// FIXME: Handle OOM failure.
|
||||
return ByteBuffer::copy(data(), length()).release_value();
|
||||
}
|
||||
|
||||
String StringBuilder::to_string() const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue