Skip to content

Commit 6e27f08

Browse files
committed
Handle OOM where possible
1 parent 013b2df commit 6e27f08

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/common.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ impl Frame<'static> {
325325
#[cfg(feature = "color_quant")]
326326
pub fn from_rgb_speed(width: u16, height: u16, pixels: &[u8], speed: i32) -> Frame<'static> {
327327
assert_eq!(width as usize * height as usize * 3, pixels.len(), "Too much or too little pixel data for the given width and height to create a GIF Frame");
328-
let mut vec: Vec<u8> = Vec::with_capacity(pixels.len() + width as usize * height as usize);
328+
let mut vec: Vec<u8> = Vec::new();
329+
vec.try_reserve_exact(pixels.len() + width as usize * height as usize).expect("OOM");
329330
for v in pixels.chunks_exact(3) {
330331
vec.extend_from_slice(&[v[0], v[1], v[2], 0xFF])
331332
}

src/encoder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,13 @@ impl<W: Write> Encoder<W> {
141141
/// if no global palette shall be used an empty slice may be supplied.
142142
pub fn new(w: W, width: u16, height: u16, global_palette: &[u8]) -> Result<Self, EncodingError> {
143143
let buffer_size = (width as usize) * (height as usize);
144+
let mut buffer = Vec::new();
145+
buffer.try_reserve_exact(buffer_size).map_err(|_| io::Error::from(io::ErrorKind::OutOfMemory))?;
144146
Encoder {
145147
w: Some(w),
146148
global_palette: false,
147-
width: width,
148-
height: height,
149-
buffer: Vec::with_capacity(buffer_size)
149+
width, height,
150+
buffer,
150151
}.write_global_palette(global_palette)
151152
}
152153

src/reader/decoder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl StreamingDecoder {
507507
let global_table = b & 0x80 != 0;
508508
let entries = if global_table {
509509
let entries = PLTE_CHANNELS*(1 << ((b & 0b111) + 1) as usize);
510-
self.global_color_table.reserve_exact(entries);
510+
self.global_color_table.try_reserve_exact(entries).map_err(|_| io::Error::from(io::ErrorKind::OutOfMemory))?;
511511
entries
512512
} else {
513513
0usize
@@ -568,9 +568,9 @@ impl StreamingDecoder {
568568

569569
if local_table {
570570
let entries = PLTE_CHANNELS * (1 << (table_size + 1));
571-
572-
self.current_frame_mut().palette =
573-
Some(Vec::with_capacity(entries));
571+
let mut pal = Vec::new();
572+
pal.try_reserve_exact(entries).map_err(|_| io::Error::from(io::ErrorKind::OutOfMemory))?;
573+
self.current_frame_mut().palette = Some(pal);
574574
goto!(LocalPalette(entries))
575575
} else {
576576
goto!(Byte(CodeSize))

src/reader/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,9 @@ impl<R> Decoder<R> where R: Read {
336336
self.current_frame.interlaced = false;
337337
}
338338
FrameDataType::Lzw { min_code_size } => {
339+
let mut vec = Vec::new();
339340
// Guesstimate 2bpp
340-
let mut vec = Vec::with_capacity(pixel_bytes/4);
341+
vec.try_reserve(pixel_bytes/4).map_err(|_| io::Error::from(io::ErrorKind::OutOfMemory))?;
341342
self.copy_lzw_into_buffer(min_code_size, &mut vec)?;
342343
self.current_frame.buffer = Cow::Owned(vec);
343344
},
@@ -380,6 +381,7 @@ impl<R> Decoder<R> where R: Read {
380381
loop {
381382
match self.decoder.decode_next(None)? {
382383
Some(Decoded::LzwData(data)) => {
384+
buf.try_reserve(data.len()).map_err(|_| io::Error::from(io::ErrorKind::OutOfMemory))?;
383385
buf.extend_from_slice(data);
384386
},
385387
Some(Decoded::DataEnd) => return Ok(()),

0 commit comments

Comments
 (0)