From 418ecbe61a1ebffc63ccae0e1f3d2f8806ea65df Mon Sep 17 00:00:00 2001 From: Tyler Date: Mon, 5 Jul 2021 11:27:17 -0700 Subject: [PATCH] Makes multiplier parsing system dependant - multipler is now created as u128, then returned as usize after conversion. Errors due to overflow now depend on the system on which the code is run. --- src/uu/dd/src/parseargs.rs | 54 +++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/uu/dd/src/parseargs.rs b/src/uu/dd/src/parseargs.rs index cc7a7447b..8a28339e0 100644 --- a/src/uu/dd/src/parseargs.rs +++ b/src/uu/dd/src/parseargs.rs @@ -293,33 +293,33 @@ impl std::str::FromStr for StatusLevel { } fn parse_multiplier<'a>(s: &'a str) -> Result { - match s { - "c" => Ok(1), - "w" => Ok(2), - "b" => Ok(512), - "kB" => Ok(1000), - "K" | "KiB" => Ok(1024), - "MB" => Ok(1000 * 1000), - "M" | "MiB" => Ok(1024 * 1024), - "GB" => Ok(1000 * 1000 * 1000), - "G" | "GiB" => Ok(1024 * 1024 * 1024), - "TB" => Ok(1000 * 1000 * 1000 * 1000), - "T" | "TiB" => Ok(1024 * 1024 * 1024 * 1024), - "PB" => Ok(1000 * 1000 * 1000 * 1000 * 1000), - "P" | "PiB" => Ok(1024 * 1024 * 1024 * 1024 * 1024), - "EB" => Ok(1000 * 1000 * 1000 * 1000 * 1000 * 1000), - "E" | "EiB" => Ok(1024 * 1024 * 1024 * 1024 * 1024 * 1024), - // The following would overflow on my x64 system - // "ZB" => - // Ok(1000*1000*1000*1000*1000*1000*1000), - // "Z" | "ZiB" => - // Ok(1024*1024*1024*1024*1024*1024*1024), - // "YB" => - // Ok(1000*1000*1000*1000*1000*1000*1000*1000), - // "Y" | "YiB" => - // Ok(1024*1024*1024*1024*1024*1024*1024*1024), - _ => Err(ParseError::NoMatchingMultiplier(s.to_string())), - } + let mult: u128 = match s { + "c" => 1, + "w" => 2, + "b" => 512, + "kB" => 1000, + "K" | "KiB" => 1024, + "MB" => 1000 * 1000, + "M" | "MiB" => 1024 * 1024, + "GB" => 1000 * 1000 * 1000, + "G" | "GiB" => 1024 * 1024 * 1024, + "TB" => 1000 * 1000 * 1000 * 1000, + "T" | "TiB" => 1024 * 1024 * 1024 * 1024, + "PB" => 1000 * 1000 * 1000 * 1000 * 1000, + "P" | "PiB" => 1024 * 1024 * 1024 * 1024 * 1024, + "EB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000, + "E" | "EiB" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024, + "ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, + "Z" | "ZiB" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, + "YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, + "Y" | "YiB" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, + _ => return Err(ParseError::NoMatchingMultiplier(s.to_string())), + }; + + mult.try_into() + .map_err(|_e| { + ParseError::MultiplierStringWouldOverflow(s.to_string()) + }) } fn parse_bytes_only(s: &str) -> Result {