Skip to content

Commit

Permalink
Use free_usize instead of a u64::try_from in gif.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
5225225 committed Apr 18, 2022
1 parent b41b4bb commit 95b53b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/codecs/gif.rs
Expand Up @@ -174,10 +174,7 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for GifDecoder<R> {

let mut frame_buffer = vec![0; buffer_size];

self.limits.free(
u64::try_from(buffer_size)
.expect("if buffer_size overflows a usize, we should have returned already"),
);
self.limits.free_usize(buffer_size);

self.reader
.read_into_buffer(&mut frame_buffer[..])
Expand Down
17 changes: 15 additions & 2 deletions src/io/mod.rs
@@ -1,5 +1,7 @@
//! Input and output of images.

use std::convert::TryFrom;

use crate::{error, ImageError, ImageResult};

pub(crate) mod free_functions;
Expand Down Expand Up @@ -124,8 +126,6 @@ impl Limits {

/// This function acts identically to [`reserve`], but takes a `usize` for convenience.
pub fn reserve_usize(&mut self, amount: usize) -> ImageResult<()> {
use std::convert::TryFrom;

match u64::try_from(amount) {
Ok(n) => self.reserve(n),
Err(_) if self.max_alloc.is_some() => {
Expand All @@ -149,4 +149,17 @@ impl Limits {
*max_alloc = max_alloc.saturating_add(amount);
}
}

/// This function acts identically to [`free`], but takes a `usize` for convenience.
pub fn free_usize(&mut self, amount: usize) {
match u64::try_from(amount) {
Ok(n) => self.free(n),
Err(_) if self.max_alloc.is_some() => {
panic!("max_alloc is set, we should have exited earlier when the reserve failed");
}
Err(_) => {
// Out of bounds, but we weren't asked to consider any limit.
}
}
}
}
2 changes: 1 addition & 1 deletion tests/regression.rs
Expand Up @@ -71,7 +71,7 @@ fn bad_gif_oom() {
// It then exits normally with an EOF when reading.
//
// So instead we look for a limits error (or an unsupported error, for the case that we're
// running these tests without bmp being actually supported)
// running these tests without gif being actually supported)
let error = image::load_from_memory(&data).unwrap_err();

assert!(
Expand Down

0 comments on commit 95b53b9

Please sign in to comment.