Skip to content

Commit e05606c

Browse files
committed
Avoid oom situations
1 parent 68b198a commit e05606c

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Cargo.lock
44
.DS_Store
55
/image.png
66
/.vscode
7+
/.idea

src/pixmap.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ impl Pixmap {
4444
let size = IntSize::from_wh(width, height)?;
4545
let data_len = data_len_for_size(size)?;
4646

47-
// We cannot check that allocation was successful yet.
48-
// We have to wait for https://github.com/rust-lang/rust/issues/48043
47+
let mut data = Vec::new();
48+
data.try_reserve_exact(data_len).ok()?;
49+
data.resize(data_len, 0);
4950

50-
Some(Pixmap {
51-
data: vec![0; data_len],
52-
size,
53-
})
51+
Some(Pixmap { data, size })
5452
}
5553

5654
/// Creates a new pixmap by taking ownership over an image buffer
@@ -81,7 +79,10 @@ impl Pixmap {
8179
let mut decoder = png::Decoder::new(data);
8280
decoder.set_transformations(png::Transformations::normalize_to_color8());
8381
let mut reader = decoder.read_info()?;
84-
let mut img_data = vec![0; reader.output_buffer_size()];
82+
let mut img_data = Vec::new();
83+
img_data.try_reserve_exact(reader.output_buffer_size())
84+
.map_err(|_| make_custom_png_error("failed to reserve output buffer"))?;
85+
img_data.resize(reader.output_buffer_size(), 0);
8586
let info = reader.next_frame(&mut img_data)?;
8687

8788
if info.bit_depth != png::BitDepth::Eight {

0 commit comments

Comments
 (0)