From 5f3885ac55d8b5e96e80867eb90a29b887d73aeb Mon Sep 17 00:00:00 2001 From: "Zhao, Gang" Date: Sat, 2 May 2026 12:39:13 +0800 Subject: [PATCH] fix possible panic in test mode It's possible some values in /proc/meminfo would be abnormally big which would overflow the multiply calculation. Didn't find related link on Internet but my Ubuntu 24.04 once showed incorrect SwapFree size which caused panic in procfs. MemTotal: 32405976 kB MemFree: 2062608 kB MemAvailable: 5264828 kB Buffers: 78156 kB Cached: 6070016 kB SwapCached: 0 kB Active: 8976496 kB Inactive: 17864952 kB Active(anon): 8207228 kB Inactive(anon): 15150596 kB Active(file): 769268 kB Inactive(file): 2714356 kB Unevictable: 1696048 kB Mlocked: 112 kB SwapTotal: 67108860 kB SwapFree: 18446744073708600176 kB ... --- procfs-core/src/meminfo.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/procfs-core/src/meminfo.rs b/procfs-core/src/meminfo.rs index 8c8e121..f300af3 100644 --- a/procfs-core/src/meminfo.rs +++ b/procfs-core/src/meminfo.rs @@ -3,12 +3,12 @@ use super::{expect, from_str, ProcResult}; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, io}; -fn convert_to_kibibytes(num: u64, unit: &str) -> ProcResult { +fn convert_to_bytes(num: u64, unit: &str) -> ProcResult { 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), + "KiB" | "kiB" | "kB" | "KB" => Ok(num.saturating_mul(1024)), + "MiB" | "miB" | "MB" | "mB" => Ok(num.saturating_mul(1024 * 1024)), + "GiB" | "giB" | "GB" | "gB" => Ok(num.saturating_mul(1024 * 1024 * 1024)), unknown => Err(build_internal_error!(format!("Unknown unit type {}", unknown))), } } @@ -321,7 +321,7 @@ impl super::FromBufRead for Meminfo { let value = from_str!(u64, value); let value = if let Some(unit) = unit { - convert_to_kibibytes(value, unit)? + convert_to_bytes(value, unit)? } else { value };