diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-09-06 03:29:52 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-06 01:53:26 +0200 |
commit | 97e97bccab085823d1365cb54142fd8c41dbcd8c (patch) | |
tree | 9008687dbcdfb6f36f6dc6372aa382b15b9d36c8 /AK | |
parent | 3a9f00c59bad7735970c72cb940d08161fda09b0 (diff) | |
download | serenity-97e97bccab085823d1365cb54142fd8c41dbcd8c.zip |
Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe
Diffstat (limited to 'AK')
-rw-r--r-- | AK/Base64.cpp | 3 | ||||
-rw-r--r-- | AK/ByteBuffer.h | 33 | ||||
-rw-r--r-- | AK/Hex.cpp | 8 | ||||
-rw-r--r-- | AK/MemoryStream.h | 5 | ||||
-rw-r--r-- | AK/String.cpp | 3 | ||||
-rw-r--r-- | AK/StringBuilder.cpp | 3 |
6 files changed, 35 insertions, 20 deletions
diff --git a/AK/Base64.cpp b/AK/Base64.cpp index 04e67e6b54..661d9c53ee 100644 --- a/AK/Base64.cpp +++ b/AK/Base64.cpp @@ -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) diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index 0edda932e4..641371cd68 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -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; } diff --git a/AK/Hex.cpp b/AK/Hex.cpp index 724108b522..099b96eb5f 100644 --- a/AK/Hex.cpp +++ b/AK/Hex.cpp @@ -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) diff --git a/AK/MemoryStream.h b/AK/MemoryStream.h index cd18d5dca4..04565932cb 100644 --- a/AK/MemoryStream.h +++ b/AK/MemoryStream.h @@ -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()); diff --git a/AK/String.cpp b/AK/String.cpp index f20a4ae61e..fcb44006c6 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -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> diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index 1cd6ce36f3..2574d02343 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -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 |