Remove several panics in write_image

Closes #35.
This commit is contained in:
Andrew Champion authored and Andreas Molzer committed 2019-04-25 20:03:29 +02:00
1 parent 4609425be9
commit 334759b08d
2 files changed
+28 -3

No files matched your search

+12 -2
View File
@@ -247,13 +247,23 @@ impl<W: Write + Seek> TiffEncoder<W> {
where
[C::Inner]: TiffValue,
{
let num_pix = (width as usize).checked_mul(height as usize)
.ok_or_else(|| std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Image width * height exceeds usize"))?;
if data.len() < num_pix {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Input data slice is undersized for provided dimensions").into());
}
let encoder = DirectoryEncoder::new(&mut self.writer)?;
let mut image: ImageEncoder<W, C> = ImageEncoder::new(encoder, width, height).unwrap();
let mut image: ImageEncoder<W, C> = ImageEncoder::new(encoder, width, height)?;
let mut idx = 0;
while image.next_strip_sample_count() > 0 {
let sample_count = image.next_strip_sample_count() as usize;
image.write_strip(&data[idx..idx + sample_count]).unwrap();
image.write_strip(&data[idx..idx + sample_count])?;
idx += sample_count;
}
image.finish()
+16 -1
View File
@@ -6,7 +6,7 @@ use tiff::encoder::{colortype, TiffEncoder};
use tiff::ColorType;
use std::fs::File;
use std::io::{Seek, SeekFrom};
use std::io::{Cursor, Seek, SeekFrom};
#[test]
fn encode_decode() {
@@ -39,6 +39,21 @@ fn encode_decode() {
}
}
#[test]
/// Test that attempting to encode when the input buffer is undersized returns
/// an error rather than panicking.
/// See: https://github.com/PistonDevelopers/image-tiff/issues/35
fn test_encode_undersized_buffer() {
let input_data = vec![1, 2, 3];
let output = Vec::new();
let mut output_stream = Cursor::new(output);
if let Ok(mut tiff) = TiffEncoder::new(&mut output_stream) {
let res = tiff.write_image::<colortype::RGB8>(50, 50, &input_data);
assert!(res.is_err());
}
}
#[test]
fn test_gray_u8_roundtrip() {
let img_file =