From 468ff8f0b9ce37694cacb926b5c3ec87721b6803 Mon Sep 17 00:00:00 2001 From: Omer Tuchfeld Date: Sun, 6 Feb 2022 21:59:59 +0100 Subject: [PATCH] Fix type-error when calling `parse_size` from od --- src/uu/od/src/inputoffset.rs | 8 ++++---- src/uu/od/src/od.rs | 18 ++++++++++-------- src/uu/od/src/parse_inputs.rs | 8 ++++---- src/uu/od/src/parse_nrofbytes.rs | 4 ++-- src/uu/od/src/partialreader.rs | 22 +++++++++++++--------- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/uu/od/src/inputoffset.rs b/src/uu/od/src/inputoffset.rs index bc12098f8..25b439291 100644 --- a/src/uu/od/src/inputoffset.rs +++ b/src/uu/od/src/inputoffset.rs @@ -11,15 +11,15 @@ pub struct InputOffset { /// The radix to print the byte offset. NoPrefix will not print a byte offset. radix: Radix, /// The current position. Initialize at `new`, increase using `increase_position`. - byte_pos: usize, + byte_pos: u64, /// An optional label printed in parentheses, typically different from `byte_pos`, /// but will increase with the same value if `byte_pos` in increased. - label: Option, + label: Option, } impl InputOffset { /// creates a new `InputOffset` using the provided values. - pub fn new(radix: Radix, byte_pos: usize, label: Option) -> Self { + pub fn new(radix: Radix, byte_pos: u64, label: Option) -> Self { Self { radix, byte_pos, @@ -28,7 +28,7 @@ impl InputOffset { } /// Increase `byte_pos` and `label` if a label is used. - pub fn increase_position(&mut self, n: usize) { + pub fn increase_position(&mut self, n: u64) { self.byte_pos += n; if let Some(l) = self.label { self.label = Some(l + n); diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index 3786e8e68..3bbe3ab5d 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -29,6 +29,7 @@ mod prn_float; mod prn_int; use std::cmp; +use std::convert::TryFrom; use crate::byteorder_io::*; use crate::formatteriteminfo::*; @@ -111,9 +112,9 @@ pub(crate) mod options { struct OdOptions { byte_order: ByteOrder, - skip_bytes: usize, - read_bytes: Option, - label: Option, + skip_bytes: u64, + read_bytes: Option, + label: Option, input_strings: Vec, formats: Vec, line_bytes: usize, @@ -148,7 +149,7 @@ impl OdOptions { }, }; - let mut label: Option = None; + let mut label: Option = None; let parsed_input = parse_inputs(matches) .map_err(|e| USimpleError::new(1, format!("Invalid inputs: {}", e)))?; @@ -170,7 +171,8 @@ impl OdOptions { 16 } else { match parse_number_of_bytes(s) { - Ok(n) => n, + Ok(n) => usize::try_from(n) + .map_err(|_| USimpleError::new(1, format!("‘{}‘ is too large", s)))?, Err(e) => { return Err(USimpleError::new( 1, @@ -569,7 +571,7 @@ where ); } - input_offset.increase_position(length); + input_offset.increase_position(length as u64); } Err(e) => { show_error!("{}", e); @@ -648,8 +650,8 @@ fn print_bytes(prefix: &str, input_decoder: &MemoryDecoder, output_info: &Output /// `read_bytes` is an optional limit to the number of bytes to read fn open_input_peek_reader( input_strings: &[String], - skip_bytes: usize, - read_bytes: Option, + skip_bytes: u64, + read_bytes: Option, ) -> PeekReader> { // should return "impl PeekRead + Read + HasError" when supported in (stable) rust let inputs = input_strings diff --git a/src/uu/od/src/parse_inputs.rs b/src/uu/od/src/parse_inputs.rs index 9d64fc732..45e664ce3 100644 --- a/src/uu/od/src/parse_inputs.rs +++ b/src/uu/od/src/parse_inputs.rs @@ -32,7 +32,7 @@ impl<'a> CommandLineOpts for ArgMatches { #[derive(PartialEq, Debug)] pub enum CommandLineInputs { FileNames(Vec), - FileAndOffset((String, usize, Option)), + FileAndOffset((String, u64, Option)), } /// Interprets the command line inputs of od. @@ -141,7 +141,7 @@ pub fn parse_inputs_traditional(input_strings: &[&str]) -> Result Result { +pub fn parse_offset_operand(s: &str) -> Result { let mut start = 0; let mut len = s.len(); let mut radix = 8; @@ -164,7 +164,7 @@ pub fn parse_offset_operand(s: &str) -> Result { radix = 10; } } - match usize::from_str_radix(&s[start..len], radix) { + match u64::from_str_radix(&s[start..len], radix) { Ok(i) => Ok(i * multiply), Err(_) => Err("parse failed"), } @@ -332,7 +332,7 @@ mod tests { .unwrap_err(); } - fn parse_offset_operand_str(s: &str) -> Result { + fn parse_offset_operand_str(s: &str) -> Result { parse_offset_operand(&String::from(s)) } diff --git a/src/uu/od/src/parse_nrofbytes.rs b/src/uu/od/src/parse_nrofbytes.rs index d6329c60a..ad00452aa 100644 --- a/src/uu/od/src/parse_nrofbytes.rs +++ b/src/uu/od/src/parse_nrofbytes.rs @@ -1,6 +1,6 @@ use uucore::parse_size::{parse_size, ParseSizeError}; -pub fn parse_number_of_bytes(s: &str) -> Result { +pub fn parse_number_of_bytes(s: &str) -> Result { let mut start = 0; let mut len = s.len(); let mut radix = 16; @@ -65,7 +65,7 @@ pub fn parse_number_of_bytes(s: &str) -> Result { _ => {} } - let factor = match usize::from_str_radix(&s[start..len], radix) { + let factor = match u64::from_str_radix(&s[start..len], radix) { Ok(f) => f, Err(e) => return Err(ParseSizeError::ParseFailure(e.to_string())), }; diff --git a/src/uu/od/src/partialreader.rs b/src/uu/od/src/partialreader.rs index 68e3f30a1..8b51d8dee 100644 --- a/src/uu/od/src/partialreader.rs +++ b/src/uu/od/src/partialreader.rs @@ -15,15 +15,15 @@ const MAX_SKIP_BUFFER: usize = 16 * 1024; /// number of bytes. pub struct PartialReader { inner: R, - skip: usize, - limit: Option, + skip: u64, + limit: Option, } impl PartialReader { /// Create a new `PartialReader` wrapping `inner`, which will skip /// `skip` bytes, and limits the output to `limit` bytes. Set `limit` /// to `None` if there should be no limit. - pub fn new(inner: R, skip: usize, limit: Option) -> Self { + pub fn new(inner: R, skip: u64, limit: Option) -> Self { Self { inner, skip, limit } } } @@ -34,7 +34,7 @@ impl Read for PartialReader { let mut bytes = [0; MAX_SKIP_BUFFER]; while self.skip > 0 { - let skip_count = cmp::min(self.skip, MAX_SKIP_BUFFER); + let skip_count: usize = cmp::min(self.skip as usize, MAX_SKIP_BUFFER); match self.inner.read(&mut bytes[..skip_count])? { 0 => { @@ -44,7 +44,7 @@ impl Read for PartialReader { "tried to skip past end of input", )); } - n => self.skip -= n, + n => self.skip -= n as u64, } } } @@ -53,15 +53,15 @@ impl Read for PartialReader { None => self.inner.read(out), Some(0) => Ok(0), Some(ref mut limit) => { - let slice = if *limit > out.len() { + let slice = if *limit > (out.len() as u64) { out } else { - &mut out[0..*limit] + &mut out[0..(*limit as usize)] }; match self.inner.read(slice) { Err(e) => Err(e), Ok(r) => { - *limit -= r; + *limit -= r as u64; Ok(r) } } @@ -145,7 +145,11 @@ mod tests { fn test_read_skipping_huge_number() { let mut v = [0; 10]; // test if it does not eat all memory.... - let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), usize::max_value(), None); + let mut sut = PartialReader::new( + Cursor::new(&b"abcdefgh"[..]), + usize::max_value() as u64, + None, + ); sut.read(v.as_mut()).unwrap_err(); }