From a4b0089e989eccdb2921ac9518dd114fbd361ff7 Mon Sep 17 00:00:00 2001 From: svartalf Date: Sun, 21 Jun 2020 22:20:43 +0300 Subject: [PATCH] Handle addition overflow when calculating CPU interrupts and DPC count stats for Windows (closes #250) --- CHANGELOG.md | 1 + heim-cpu/src/sys/windows/stats.rs | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83610e38..09bc564a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ for information about previous releases. ### Fixed * `cpu::times` for Linux correctly parses `/proc/stat` (#233) + * Handle addition overflow when calculating CPU interrupts stats for Windows (#250) ## Older versions diff --git a/heim-cpu/src/sys/windows/stats.rs b/heim-cpu/src/sys/windows/stats.rs index f5f57e19..b461bc8e 100644 --- a/heim-cpu/src/sys/windows/stats.rs +++ b/heim-cpu/src/sys/windows/stats.rs @@ -44,20 +44,29 @@ fn system_performance_info() -> Result<(u64, u64)> { fn dpc_count() -> Result { let info: Vec = winternl::query_system_information()?; - let count = info.into_iter().fold(0, |acc, item| acc + item.DpcCount); + let count = info.into_iter() + .fold(0u64, |acc, item| { + // TODO: Log the overflow (`info` level?) + acc.overflowing_add(item.DpcCount.into()).0 + }); - Ok(count.into()) + Ok(count) } fn interrupts() -> Result { let info: Vec = winternl::query_system_information()?; - let count = info - .into_iter() - .fold(0, |acc, item| acc + item.InterruptCount); + let count = info.into_iter().fold(0u64, |acc, item| { + // `InterruptCount` type is `u32` (`ULONG`) and working with `u32` + // in here can overflow really quick (see #250). + // `u64` will not overflow that fast, but we still want + // to handle that case. + // TODO: Log the overflow (`info` level?) + acc.overflowing_add(item.InterruptCount.into()).0 + }); - Ok(count.into()) + Ok(count) } pub async fn stats() -> Result {