1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

Fix type-error when calling parse_size from od

This commit is contained in:
Omer Tuchfeld 2022-02-06 21:59:59 +01:00
parent 6856c5dba5
commit 468ff8f0b9
5 changed files with 33 additions and 27 deletions

View file

@ -11,15 +11,15 @@ pub struct InputOffset {
/// The radix to print the byte offset. NoPrefix will not print a byte offset. /// The radix to print the byte offset. NoPrefix will not print a byte offset.
radix: Radix, radix: Radix,
/// The current position. Initialize at `new`, increase using `increase_position`. /// 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`, /// An optional label printed in parentheses, typically different from `byte_pos`,
/// but will increase with the same value if `byte_pos` in increased. /// but will increase with the same value if `byte_pos` in increased.
label: Option<usize>, label: Option<u64>,
} }
impl InputOffset { impl InputOffset {
/// creates a new `InputOffset` using the provided values. /// creates a new `InputOffset` using the provided values.
pub fn new(radix: Radix, byte_pos: usize, label: Option<usize>) -> Self { pub fn new(radix: Radix, byte_pos: u64, label: Option<u64>) -> Self {
Self { Self {
radix, radix,
byte_pos, byte_pos,
@ -28,7 +28,7 @@ impl InputOffset {
} }
/// Increase `byte_pos` and `label` if a label is used. /// 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; self.byte_pos += n;
if let Some(l) = self.label { if let Some(l) = self.label {
self.label = Some(l + n); self.label = Some(l + n);

View file

@ -29,6 +29,7 @@ mod prn_float;
mod prn_int; mod prn_int;
use std::cmp; use std::cmp;
use std::convert::TryFrom;
use crate::byteorder_io::*; use crate::byteorder_io::*;
use crate::formatteriteminfo::*; use crate::formatteriteminfo::*;
@ -111,9 +112,9 @@ pub(crate) mod options {
struct OdOptions { struct OdOptions {
byte_order: ByteOrder, byte_order: ByteOrder,
skip_bytes: usize, skip_bytes: u64,
read_bytes: Option<usize>, read_bytes: Option<u64>,
label: Option<usize>, label: Option<u64>,
input_strings: Vec<String>, input_strings: Vec<String>,
formats: Vec<ParsedFormatterItemInfo>, formats: Vec<ParsedFormatterItemInfo>,
line_bytes: usize, line_bytes: usize,
@ -148,7 +149,7 @@ impl OdOptions {
}, },
}; };
let mut label: Option<usize> = None; let mut label: Option<u64> = None;
let parsed_input = parse_inputs(matches) let parsed_input = parse_inputs(matches)
.map_err(|e| USimpleError::new(1, format!("Invalid inputs: {}", e)))?; .map_err(|e| USimpleError::new(1, format!("Invalid inputs: {}", e)))?;
@ -170,7 +171,8 @@ impl OdOptions {
16 16
} else { } else {
match parse_number_of_bytes(s) { 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) => { Err(e) => {
return Err(USimpleError::new( return Err(USimpleError::new(
1, 1,
@ -569,7 +571,7 @@ where
); );
} }
input_offset.increase_position(length); input_offset.increase_position(length as u64);
} }
Err(e) => { Err(e) => {
show_error!("{}", 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 /// `read_bytes` is an optional limit to the number of bytes to read
fn open_input_peek_reader( fn open_input_peek_reader(
input_strings: &[String], input_strings: &[String],
skip_bytes: usize, skip_bytes: u64,
read_bytes: Option<usize>, read_bytes: Option<u64>,
) -> PeekReader<PartialReader<MultifileReader>> { ) -> PeekReader<PartialReader<MultifileReader>> {
// should return "impl PeekRead + Read + HasError" when supported in (stable) rust // should return "impl PeekRead + Read + HasError" when supported in (stable) rust
let inputs = input_strings let inputs = input_strings

View file

@ -32,7 +32,7 @@ impl<'a> CommandLineOpts for ArgMatches {
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub enum CommandLineInputs { pub enum CommandLineInputs {
FileNames(Vec<String>), FileNames(Vec<String>),
FileAndOffset((String, usize, Option<usize>)), FileAndOffset((String, u64, Option<u64>)),
} }
/// Interprets the command line inputs of od. /// Interprets the command line inputs of od.
@ -141,7 +141,7 @@ pub fn parse_inputs_traditional(input_strings: &[&str]) -> Result<CommandLineInp
} }
/// parses format used by offset and label on the command line /// parses format used by offset and label on the command line
pub fn parse_offset_operand(s: &str) -> Result<usize, &'static str> { pub fn parse_offset_operand(s: &str) -> Result<u64, &'static str> {
let mut start = 0; let mut start = 0;
let mut len = s.len(); let mut len = s.len();
let mut radix = 8; let mut radix = 8;
@ -164,7 +164,7 @@ pub fn parse_offset_operand(s: &str) -> Result<usize, &'static str> {
radix = 10; 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), Ok(i) => Ok(i * multiply),
Err(_) => Err("parse failed"), Err(_) => Err("parse failed"),
} }
@ -332,7 +332,7 @@ mod tests {
.unwrap_err(); .unwrap_err();
} }
fn parse_offset_operand_str(s: &str) -> Result<usize, &'static str> { fn parse_offset_operand_str(s: &str) -> Result<u64, &'static str> {
parse_offset_operand(&String::from(s)) parse_offset_operand(&String::from(s))
} }

View file

@ -1,6 +1,6 @@
use uucore::parse_size::{parse_size, ParseSizeError}; use uucore::parse_size::{parse_size, ParseSizeError};
pub fn parse_number_of_bytes(s: &str) -> Result<usize, ParseSizeError> { pub fn parse_number_of_bytes(s: &str) -> Result<u64, ParseSizeError> {
let mut start = 0; let mut start = 0;
let mut len = s.len(); let mut len = s.len();
let mut radix = 16; let mut radix = 16;
@ -65,7 +65,7 @@ pub fn parse_number_of_bytes(s: &str) -> Result<usize, ParseSizeError> {
_ => {} _ => {}
} }
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, Ok(f) => f,
Err(e) => return Err(ParseSizeError::ParseFailure(e.to_string())), Err(e) => return Err(ParseSizeError::ParseFailure(e.to_string())),
}; };

View file

@ -15,15 +15,15 @@ const MAX_SKIP_BUFFER: usize = 16 * 1024;
/// number of bytes. /// number of bytes.
pub struct PartialReader<R> { pub struct PartialReader<R> {
inner: R, inner: R,
skip: usize, skip: u64,
limit: Option<usize>, limit: Option<u64>,
} }
impl<R> PartialReader<R> { impl<R> PartialReader<R> {
/// Create a new `PartialReader` wrapping `inner`, which will skip /// Create a new `PartialReader` wrapping `inner`, which will skip
/// `skip` bytes, and limits the output to `limit` bytes. Set `limit` /// `skip` bytes, and limits the output to `limit` bytes. Set `limit`
/// to `None` if there should be no limit. /// to `None` if there should be no limit.
pub fn new(inner: R, skip: usize, limit: Option<usize>) -> Self { pub fn new(inner: R, skip: u64, limit: Option<u64>) -> Self {
Self { inner, skip, limit } Self { inner, skip, limit }
} }
} }
@ -34,7 +34,7 @@ impl<R: Read> Read for PartialReader<R> {
let mut bytes = [0; MAX_SKIP_BUFFER]; let mut bytes = [0; MAX_SKIP_BUFFER];
while self.skip > 0 { 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])? { match self.inner.read(&mut bytes[..skip_count])? {
0 => { 0 => {
@ -44,7 +44,7 @@ impl<R: Read> Read for PartialReader<R> {
"tried to skip past end of input", "tried to skip past end of input",
)); ));
} }
n => self.skip -= n, n => self.skip -= n as u64,
} }
} }
} }
@ -53,15 +53,15 @@ impl<R: Read> Read for PartialReader<R> {
None => self.inner.read(out), None => self.inner.read(out),
Some(0) => Ok(0), Some(0) => Ok(0),
Some(ref mut limit) => { Some(ref mut limit) => {
let slice = if *limit > out.len() { let slice = if *limit > (out.len() as u64) {
out out
} else { } else {
&mut out[0..*limit] &mut out[0..(*limit as usize)]
}; };
match self.inner.read(slice) { match self.inner.read(slice) {
Err(e) => Err(e), Err(e) => Err(e),
Ok(r) => { Ok(r) => {
*limit -= r; *limit -= r as u64;
Ok(r) Ok(r)
} }
} }
@ -145,7 +145,11 @@ mod tests {
fn test_read_skipping_huge_number() { fn test_read_skipping_huge_number() {
let mut v = [0; 10]; let mut v = [0; 10];
// test if it does not eat all memory.... // 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(); sut.read(v.as_mut()).unwrap_err();
} }