1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
--- a/src/meminfo.rs
+++ b/src/meminfo.rs
@@ -4,13 +4,17 @@
use std::{collections::HashMap, io};
fn convert_to_kibibytes(num: u64, unit: &str) -> ProcResult<u64> {
- match unit {
- "B" => Ok(num),
- "KiB" | "kiB" | "kB" | "KB" => Ok(num * 1024),
- "MiB" | "miB" | "MB" | "mB" => Ok(num * 1024 * 1024),
- "GiB" | "giB" | "GB" | "gB" => Ok(num * 1024 * 1024 * 1024),
- unknown => Err(build_internal_error!(format!("Unknown unit type {}", unknown))),
- }
+ let result = match unit {
+ "B" => num as u128,
+ "KiB" | "kiB" | "kB" | "KB" => num as u128 * 1024,
+ "MiB" | "miB" | "MB" | "mB" => num as u128 * 1024 * 1024,
+ "GiB" | "giB" | "GB" | "gB" => num as u128 * 1024 * 1024 * 1024,
+ unknown => return Err(build_internal_error!(format!("Unknown unit type {}", unknown))),
+ };
+ if result > u64::MAX.into() {
+ return Err(build_internal_error!(format!("arithmetic overflow while converting value {} with unit {}", num, unit)));
+ }
+ return Ok(result as u64);
}
/// This struct reports statistics about memory usage on the system, based on
|