mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:07:35 +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
|
@ -946,7 +946,12 @@ static bool uncompress_bmp_rle_data(BMPLoadingContext& context, ByteBuffer& buff
|
|||
dbgln("Suspiciously large amount of RLE data");
|
||||
return false;
|
||||
}
|
||||
buffer = ByteBuffer::create_zeroed(buffer_size);
|
||||
auto buffer_result = ByteBuffer::create_zeroed(buffer_size);
|
||||
if (!buffer_result.has_value()) {
|
||||
dbgln("Not enough memory for buffer allocation");
|
||||
return false;
|
||||
}
|
||||
buffer = buffer_result.release_value();
|
||||
|
||||
// Avoid as many if statements as possible by pulling out
|
||||
// compression-dependent actions into separate lambdas
|
||||
|
|
|
@ -45,7 +45,11 @@ private:
|
|||
static ByteBuffer write_pixel_data(const RefPtr<Bitmap> bitmap, int pixel_row_data_size, int bytes_per_pixel, bool include_alpha_channel)
|
||||
{
|
||||
int image_size = pixel_row_data_size * bitmap->height();
|
||||
auto buffer = ByteBuffer::create_uninitialized(image_size);
|
||||
auto buffer_result = ByteBuffer::create_uninitialized(image_size);
|
||||
if (!buffer_result.has_value())
|
||||
return {};
|
||||
|
||||
auto buffer = buffer_result.release_value();
|
||||
|
||||
int current_row = 0;
|
||||
for (int y = bitmap->physical_height() - 1; y >= 0; --y) {
|
||||
|
@ -95,7 +99,11 @@ ByteBuffer BMPWriter::dump(const RefPtr<Bitmap> bitmap, DibHeader dib_header)
|
|||
|
||||
int pixel_row_data_size = (m_bytes_per_pixel * 8 * bitmap->width() + 31) / 32 * 4;
|
||||
int image_size = pixel_row_data_size * bitmap->height();
|
||||
auto buffer = ByteBuffer::create_uninitialized(pixel_data_offset);
|
||||
auto buffer_result = ByteBuffer::create_uninitialized(pixel_data_offset);
|
||||
if (!buffer_result.has_value())
|
||||
return {};
|
||||
|
||||
auto buffer = buffer_result.release_value();
|
||||
|
||||
auto pixel_data = write_pixel_data(bitmap, pixel_row_data_size, m_bytes_per_pixel, m_include_alpha_channel);
|
||||
pixel_data = compress_pixel_data(pixel_data, m_compression);
|
||||
|
|
|
@ -247,7 +247,8 @@ RefPtr<Bitmap> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffe
|
|||
|
||||
ByteBuffer Bitmap::serialize_to_byte_buffer() const
|
||||
{
|
||||
auto buffer = ByteBuffer::create_uninitialized(sizeof(size_t) + 4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(RGBA32) * palette_size(m_format) + size_in_bytes());
|
||||
// FIXME: Somehow handle possible OOM situation here.
|
||||
auto buffer = ByteBuffer::create_uninitialized(sizeof(size_t) + 4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(RGBA32) * palette_size(m_format) + size_in_bytes()).release_value();
|
||||
OutputMemoryStream stream { buffer };
|
||||
|
||||
auto write = [&]<typename T>(T value) {
|
||||
|
|
|
@ -221,7 +221,8 @@ ByteBuffer PNGWriter::encode(Gfx::Bitmap const& bitmap)
|
|||
writer.add_IHDR_chunk(bitmap.width(), bitmap.height(), 8, 6, 0, 0, 0);
|
||||
writer.add_IDAT_chunk(bitmap);
|
||||
writer.add_IEND_chunk();
|
||||
return ByteBuffer::copy(writer.m_data);
|
||||
// FIXME: Handle OOM failure.
|
||||
return ByteBuffer::copy(writer.m_data).release_value();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue