From 583b241cef15e59507a6187bb58a6c38e682402f Mon Sep 17 00:00:00 2001 From: zroug <37004975+zroug@users.noreply.github.com> Date: Mon, 27 Aug 2018 23:05:16 +0200 Subject: [PATCH] Replaced unsafe code in Decoder::read_image --- src/decoder/mod.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 045944a..2495de4 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -467,8 +467,8 @@ impl Decoder { * self.height as usize * self.bits_per_sample.iter().count(); let mut result = match self.bits_per_sample.iter().cloned().max().unwrap_or(8) { - n if n <= 8 => DecodingResult::U8(Vec::with_capacity(buffer_size)), - n if n <= 16 => DecodingResult::U16(Vec::with_capacity(buffer_size)), + n if n <= 8 => DecodingResult::U8(vec![0; buffer_size]), + n if n <= 16 => DecodingResult::U16(vec![0; buffer_size]), n => return Err( TiffError::UnsupportedError( format!("{} bits per channel not supported", n) @@ -483,13 +483,6 @@ impl Decoder { )) } } - // Safe since the uninitialized values are never read. - match result { - DecodingResult::U8(ref mut buffer) => - unsafe { buffer.set_len(buffer_size) }, - DecodingResult::U16(ref mut buffer) => - unsafe { buffer.set_len(buffer_size) }, - } let mut units_read = 0; for (i, (&offset, &byte_count)) in try!(self.get_tag_u32_vec(ifd::Tag::StripOffsets)) .iter().zip(try!(self.get_tag_u32_vec(ifd::Tag::StripByteCounts)).iter()).enumerate() { @@ -514,14 +507,8 @@ impl Decoder { break } } - // Shrink length such that the uninitialized memory is not exposed. if units_read < buffer_size { - match result { - DecodingResult::U8(ref mut buffer) => - unsafe { buffer.set_len(units_read) }, - DecodingResult::U16(ref mut buffer) => - unsafe { buffer.set_len(units_read) }, - } + return Err(TiffError::FormatError("Inconsistent sizes encountered.".to_string())); } if let Ok(predictor) = self.get_tag_u32(ifd::Tag::Predictor) { result = match FromPrimitive::from_u32(predictor) {