From 5c3f50f8009899c093d2823f5f421a95268aed9c Mon Sep 17 00:00:00 2001 From: Birk Tjelmeland Date: Thu, 28 Feb 2019 07:58:07 +0100 Subject: [PATCH] Fix compiler and clippy warnings --- src/decoder/ifd.rs | 8 ++++---- src/decoder/mod.rs | 31 ++++++++++++++----------------- tests/fuzz_tests.rs | 4 ++-- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 474db41..3f0ff69 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -171,7 +171,7 @@ impl Entry { }, (Type::SHORT, n) => { // each value is ~8 bytes - if n as u64*8 > limits.decoding_buffer_size { + if u64::from(n)*8 > limits.decoding_buffer_size { return Err(TiffError::LimitsExceeded) } let mut v = Vec::with_capacity(n as usize); @@ -184,7 +184,7 @@ impl Entry { (Type::LONG, 1) => Ok(Unsigned(try!(self.r(bo).read_u32()))), (Type::LONG, n) => { // each value is ~8 bytes - if n as u64*8 > limits.decoding_buffer_size { + if u64::from(n)*8 > limits.decoding_buffer_size { return Err(TiffError::LimitsExceeded) } let mut v = Vec::with_capacity(n as usize); @@ -202,7 +202,7 @@ impl Entry { }, (Type::RATIONAL, n) => { // each value is ~8 bytes - if n as u64*8 > limits.decoding_buffer_size { + if u64::from(n)*8 > limits.decoding_buffer_size { return Err(TiffError::LimitsExceeded) } let mut v = Vec::with_capacity(n as usize); @@ -215,7 +215,7 @@ impl Entry { Ok(List(v)) }, (Type::ASCII, n) => { - if n as u64 > limits.decoding_buffer_size { + if u64::from(n) > limits.decoding_buffer_size { return Err(TiffError::LimitsExceeded) } try!(decoder.goto_offset(try!(self.r(bo).read_u32()))); diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index c846657..b58649f 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1,7 +1,6 @@ use std::io::{self, Read, Seek}; use num_traits::{FromPrimitive, Num}; use std::collections::HashMap; -use std::string::FromUtf8Error; use ::{ColorType, TiffError, TiffFormatError, TiffUnsupportedError, TiffResult}; @@ -46,7 +45,7 @@ impl DecodingResult { } } - pub fn to_buffer(&mut self, start: usize) -> DecodingBuffer { + pub fn as_buffer(&mut self, start: usize) -> DecodingBuffer { match self { DecodingResult::U8(ref mut buf) => DecodingBuffer::U8(&mut buf[start..]), DecodingResult::U16(ref mut buf) => DecodingBuffer::U16(&mut buf[start..]), @@ -72,8 +71,8 @@ impl<'a> DecodingBuffer<'a> { fn byte_len(&self) -> usize { match self { - DecodingBuffer::U8(ref buf) => 1, - DecodingBuffer::U16(ref buf) => 2, + DecodingBuffer::U8(_) => 1, + DecodingBuffer::U16(_) => 2, } } @@ -206,14 +205,15 @@ fn rev_hpredict(image: DecodingBuffer, size: (u32, u32), color_type: ColorType) ColorType::RGBA(8) | ColorType::RGBA(16) | ColorType::CMYK(8) => 4, _ => return Err(TiffError::UnsupportedError(TiffUnsupportedError::HorizontalPredictor(color_type))) }; - Ok(match image { + match image { DecodingBuffer::U8(buf) => { - rev_hpredict_nsamp(buf, size, samples) + rev_hpredict_nsamp(buf, size, samples); }, DecodingBuffer::U16(buf) => { - rev_hpredict_nsamp(buf, size, samples) + rev_hpredict_nsamp(buf, size, samples); } - }) + } + Ok(()) } impl Decoder { @@ -332,10 +332,7 @@ impl Decoder { /// Returns `true` if there is at least one more image available. pub fn more_images(&self) -> bool { - match self.next_ifd { - Some(_) => true, - None => false - } + self.next_ifd.is_some() } /// Returns the byte_order @@ -357,12 +354,12 @@ impl Decoder { /// Reads a string #[inline] - pub fn read_string(&mut self, length: usize) -> Result { + pub fn read_string(&mut self, length: usize) -> TiffResult { let mut out = String::with_capacity(length); - self.reader.read_to_string(&mut out); + self.reader.read_to_string(&mut out)?; // Strings may be null-terminated, so we trim anything downstream of the null byte let trimmed = out.bytes().take_while(|&n| n != 0).collect::>(); - String::from_utf8(trimmed) + Ok(String::from_utf8(trimmed)?) } /// Reads a TIFF IFA offset/value field @@ -621,7 +618,7 @@ impl Decoder { n => return Err(TiffError::UnsupportedError(TiffUnsupportedError::UnsupportedBitsPerChannel(n))), }; - self.read_strip_to_buffer(result.to_buffer(0))?; + self.read_strip_to_buffer(result.as_buffer(0))?; Ok(result) } @@ -643,7 +640,7 @@ impl Decoder { }; for i in 0..self.strip_count()? as usize { - self.read_strip_to_buffer(result.to_buffer(samples_per_strip * i))?; + self.read_strip_to_buffer(result.as_buffer(samples_per_strip * i))?; } Ok(result) diff --git a/tests/fuzz_tests.rs b/tests/fuzz_tests.rs index f267285..88cb64d 100644 --- a/tests/fuzz_tests.rs +++ b/tests/fuzz_tests.rs @@ -1,7 +1,7 @@ extern crate tiff; -use tiff::{TiffResult, ColorType}; -use tiff::decoder::{Decoder, DecodingResult, ifd::Tag, ifd::Value}; +use tiff::TiffResult; +use tiff::decoder::Decoder; use std::fs::File;