From e8d7d1ab86a169cb6582ff6f95a328a7fddf54b7 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 16 May 2025 01:42:08 +0300 Subject: [PATCH] cpu: prevent possible overflow in frequency calculations --- src/cpu.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index b17bb59..1932efe 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -309,7 +309,10 @@ pub fn set_min_frequency(freq_mhz: u32, core_id: Option) -> Result<()> { } } - let freq_khz_str = (freq_mhz * 1000).to_string(); + // XXX: We use u64 for the intermediate calculation to prevent overflow + let freq_khz = u64::from(freq_mhz) * 1000; + let freq_khz_str = freq_khz.to_string(); + let action = |id: u32| { let path = format!("/sys/devices/system/cpu/cpu{id}/cpufreq/scaling_min_freq"); if Path::new(&path).exists() { @@ -333,7 +336,10 @@ pub fn set_max_frequency(freq_mhz: u32, core_id: Option) -> Result<()> { } } - let freq_khz_str = (freq_mhz * 1000).to_string(); + // XXX: Use a u64 here as well. + let freq_khz = u64::from(freq_mhz) * 1000; + let freq_khz_str = freq_khz.to_string(); + let action = |id: u32| { let path = format!("/sys/devices/system/cpu/cpu{id}/cpufreq/scaling_max_freq"); if Path::new(&path).exists() {