1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:57:36 +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

@ -80,8 +80,7 @@ void GlyphEditorWidget::copy_glyph()
metadata.set("width", String::number(bitmap.width()));
metadata.set("height", String::number(bitmap.height()));
auto data = ByteBuffer::copy(&bits[0], bitmap.width() * bitmap.height());
GUI::Clipboard::the().set_data(data, "glyph/x-fonteditor", metadata);
GUI::Clipboard::the().set_data(ReadonlyBytes(&bits[0], bitmap.width() * bitmap.height()), "glyph/x-fonteditor", metadata);
}
void GlyphEditorWidget::paste_glyph()

View file

@ -75,7 +75,12 @@ HexEditorWidget::HexEditorWidget()
auto file_size = value.to_int();
if (file_size.has_value() && file_size.value() > 0) {
m_document_dirty = false;
m_editor->set_buffer(ByteBuffer::create_zeroed(file_size.value()));
auto buffer_result = ByteBuffer::create_zeroed(file_size.value());
if (!buffer_result.has_value()) {
GUI::MessageBox::show(window(), "Entered file size is too large.", "Error", GUI::MessageBox::Type::Error);
return;
}
m_editor->set_buffer(buffer_result.release_value());
set_path({});
update_title();
} else {

View file

@ -242,8 +242,7 @@ void ViewWidget::load_from_file(const String& path)
// Spawn a new ImageDecoder service process and connect to it.
auto client = ImageDecoderClient::Client::construct();
// FIXME: Find a way to avoid the memory copying here.
auto decoded_image_or_error = client->decode_image(ByteBuffer::copy(mapped_file.bytes()));
auto decoded_image_or_error = client->decode_image(mapped_file.bytes());
if (!decoded_image_or_error.has_value()) {
show_error();
return;

View file

@ -56,7 +56,7 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect) con
}
}
RefPtr<Gfx::Bitmap> Image::try_decode_bitmap(ByteBuffer const& bitmap_data)
RefPtr<Gfx::Bitmap> Image::try_decode_bitmap(ReadonlyBytes const& bitmap_data)
{
// Spawn a new ImageDecoder service process and connect to it.
auto client = ImageDecoderClient::Client::construct();

View file

@ -50,7 +50,7 @@ public:
static Result<NonnullRefPtr<Image>, String> try_create_from_pixel_paint_json(JsonObject const&);
static RefPtr<Image> try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap>);
static RefPtr<Gfx::Bitmap> try_decode_bitmap(const ByteBuffer& bitmap_data);
static RefPtr<Gfx::Bitmap> try_decode_bitmap(const ReadonlyBytes& bitmap_data);
// This generates a new Bitmap with the final image (all layers composed according to their attributes.)
RefPtr<Gfx::Bitmap> try_compose_bitmap(Gfx::BitmapFormat format) const;

View file

@ -35,7 +35,7 @@ Result<void, String> ProjectLoader::try_load_from_fd_and_close(int fd, StringVie
auto& mapped_file = *file_or_error.value();
// FIXME: Find a way to avoid the memory copy here.
auto bitmap = Image::try_decode_bitmap(ByteBuffer::copy(mapped_file.bytes()));
auto bitmap = Image::try_decode_bitmap(mapped_file.bytes());
if (!bitmap)
return String { "Unable to decode image"sv };
auto image = Image::try_create_from_bitmap(bitmap.release_nonnull());

View file

@ -35,7 +35,7 @@ CSVExportDialogPage::CSVExportDialogPage(const Sheet& sheet)
m_headers.extend(m_data.take_first());
auto temp_template = String::formatted("{}/spreadsheet-csv-export.{}.XXXXXX", Core::StandardPaths::tempfile_directory(), getpid());
auto temp_path = ByteBuffer::create_uninitialized(temp_template.length() + 1);
auto temp_path = ByteBuffer::create_uninitialized(temp_template.length() + 1).release_value();
auto buf = reinterpret_cast<char*>(temp_path.data());
auto copy_ok = temp_template.copy_characters_to_buffer(buf, temp_path.size());
VERIFY(copy_ok);

View file

@ -85,7 +85,7 @@ BENCHMARK_CASE(fairly_big_data)
{
constexpr auto num_rows = 100000u;
constexpr auto line = "well,hello,friends,1,2,3,4,5,6,7,8,pizza,guacamole\n"sv;
auto buf = ByteBuffer::create_uninitialized((line.length() * num_rows) + 1);
auto buf = ByteBuffer::create_uninitialized((line.length() * num_rows) + 1).release_value();
buf[buf.size() - 1] = '\0';
for (size_t row = 0; row <= num_rows; ++row) {

View file

@ -18,7 +18,7 @@ TEST_CASE(can_write)
{ 7, 8, 9 },
};
auto buffer = ByteBuffer::create_uninitialized(1024);
auto buffer = ByteBuffer::create_uninitialized(1024).release_value();
OutputMemoryStream stream { buffer };
Writer::CSV csv(stream, data);
@ -39,7 +39,7 @@ TEST_CASE(can_write_with_header)
{ 7, 8, 9 },
};
auto buffer = ByteBuffer::create_uninitialized(1024);
auto buffer = ByteBuffer::create_uninitialized(1024).release_value();
OutputMemoryStream stream { buffer };
Writer::CSV csv(stream, data, { "A", "B\"", "C" });
@ -60,7 +60,7 @@ TEST_CASE(can_write_with_different_behaviours)
{ "We\"ll", "Hello,", " Friends" },
};
auto buffer = ByteBuffer::create_uninitialized(1024);
auto buffer = ByteBuffer::create_uninitialized(1024).release_value();
OutputMemoryStream stream { buffer };
Writer::CSV csv(stream, data, { "A", "B\"", "C" }, Writer::WriterBehaviour::QuoteOnlyInFieldStart | Writer::WriterBehaviour::WriteHeaders);