Replaced unsafe code in Decoder::read_image
This commit is contained in:
1 parent
daf23f34a0
commit
583b241cef
1 file changed
+3
-16
+3
-16
@@ -467,8 +467,8 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
* self.height as usize
|
* self.height as usize
|
||||||
* self.bits_per_sample.iter().count();
|
* self.bits_per_sample.iter().count();
|
||||||
let mut result = match self.bits_per_sample.iter().cloned().max().unwrap_or(8) {
|
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 <= 8 => DecodingResult::U8(vec![0; buffer_size]),
|
||||||
n if n <= 16 => DecodingResult::U16(Vec::with_capacity(buffer_size)),
|
n if n <= 16 => DecodingResult::U16(vec![0; buffer_size]),
|
||||||
n => return Err(
|
n => return Err(
|
||||||
TiffError::UnsupportedError(
|
TiffError::UnsupportedError(
|
||||||
format!("{} bits per channel not supported", n)
|
format!("{} bits per channel not supported", n)
|
||||||
@@ -483,13 +483,6 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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;
|
let mut units_read = 0;
|
||||||
for (i, (&offset, &byte_count)) in try!(self.get_tag_u32_vec(ifd::Tag::StripOffsets))
|
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() {
|
.iter().zip(try!(self.get_tag_u32_vec(ifd::Tag::StripByteCounts)).iter()).enumerate() {
|
||||||
@@ -514,14 +507,8 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Shrink length such that the uninitialized memory is not exposed.
|
|
||||||
if units_read < buffer_size {
|
if units_read < buffer_size {
|
||||||
match result {
|
return Err(TiffError::FormatError("Inconsistent sizes encountered.".to_string()));
|
||||||
DecodingResult::U8(ref mut buffer) =>
|
|
||||||
unsafe { buffer.set_len(units_read) },
|
|
||||||
DecodingResult::U16(ref mut buffer) =>
|
|
||||||
unsafe { buffer.set_len(units_read) },
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if let Ok(predictor) = self.get_tag_u32(ifd::Tag::Predictor) {
|
if let Ok(predictor) = self.get_tag_u32(ifd::Tag::Predictor) {
|
||||||
result = match FromPrimitive::from_u32(predictor) {
|
result = match FromPrimitive::from_u32(predictor) {
|
||||||
|
|||||||
Reference in new issue
Block a user