1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:28:11 +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

@ -134,11 +134,11 @@ bool FlacLoaderPlugin::parse_header()
ok = ok && (m_total_samples > 0);
CHECK_OK("Number of samples");
// Parse checksum into a buffer first
ByteBuffer md5_checksum = ByteBuffer::create_uninitialized(128 / 8);
Array<u8, 128 / 8> md5_checksum;
auto md5_bytes_read = streaminfo_data.read(md5_checksum);
ok = ok && (md5_bytes_read == md5_checksum.size());
CHECK_OK("MD5 Checksum");
md5_checksum.bytes().copy_to({ m_md5_checksum, sizeof(m_md5_checksum) });
md5_checksum.span().copy_to({ m_md5_checksum, sizeof(m_md5_checksum) });
// Parse other blocks
// TODO: For a simple first implementation, all other blocks are skipped as allowed by the FLAC specification.
@ -195,7 +195,12 @@ FlacRawMetadataBlock FlacLoaderPlugin::next_meta_block(InputBitStream& bit_input
u32 block_length = bit_input.read_bits_big_endian(24);
m_data_start_location += 3;
CHECK_IO_ERROR();
ByteBuffer block_data = ByteBuffer::create_uninitialized(block_length);
auto block_data_result = ByteBuffer::create_uninitialized(block_length);
if (!block_data_result.has_value()) {
m_error_string = "Out of memory";
return FlacRawMetadataBlock {};
}
auto block_data = block_data_result.release_value();
// Reads exactly the bytes necessary into the Bytes container
bit_input.read(block_data);
m_data_start_location += block_length;
@ -544,7 +549,7 @@ FlacSubframeHeader FlacLoaderPlugin::next_subframe_header(InputBitStream& bit_st
FlacSubframeType subframe_type;
u8 order = 0;
//LPC has the highest bit set
// LPC has the highest bit set
if ((subframe_code & 0b100000) > 0) {
subframe_type = FlacSubframeType::LPC;
order = (subframe_code & 0b011111) + 1;
@ -808,9 +813,10 @@ ALWAYS_INLINE i32 decode_unsigned_exp_golomb(u8 k, InputBitStream& bit_input)
u64 read_utf8_char(InputStream& input)
{
u64 character;
ByteBuffer single_byte_buffer = ByteBuffer::create_uninitialized(1);
input.read(single_byte_buffer);
u8 start_byte = single_byte_buffer[0];
u8 buffer = 0;
Bytes buffer_bytes { &buffer, 1 };
input.read(buffer_bytes);
u8 start_byte = buffer_bytes[0];
// Signal byte is zero: ASCII character
if ((start_byte & 0b10000000) == 0) {
return start_byte;
@ -826,8 +832,8 @@ u64 read_utf8_char(InputStream& input)
u8 start_byte_bitmask = AK::exp2(bits_from_start_byte) - 1;
character = start_byte_bitmask & start_byte;
for (u8 i = length - 1; i > 0; --i) {
input.read(single_byte_buffer);
u8 current_byte = single_byte_buffer[0];
input.read(buffer_bytes);
u8 current_byte = buffer_bytes[0];
character = (character << 6) | (current_byte & 0b00111111);
}
return character;