From 8577186e7804a0006c7da9915457e8d29745efb1 Mon Sep 17 00:00:00 2001 From: racequite Date: Sun, 2 Aug 2026 01:47:33 +0800 Subject: [PATCH] wasmtime-cache: Reject overflowing duration values --- crates/cache/src/config.rs | 6 +++--- crates/cache/src/config/tests.rs | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/cache/src/config.rs b/crates/cache/src/config.rs index beef58d28270..bc39f87a78cc 100644 --- a/crates/cache/src/config.rs +++ b/crates/cache/src/config.rs @@ -262,9 +262,9 @@ macro_rules! generate_deserializer { generate_deserializer!(deserialize_duration(num: u64, unit: &str) -> Duration { match unit { "s" => Some(Duration::from_secs(num)), - "m" => Some(Duration::from_secs(num * 60)), - "h" => Some(Duration::from_secs(num * 60 * 60)), - "d" => Some(Duration::from_secs(num * 60 * 60 * 24)), + "m" => num.checked_mul(60).map(Duration::from_secs), + "h" => num.checked_mul(60 * 60).map(Duration::from_secs), + "d" => num.checked_mul(60 * 60 * 24).map(Duration::from_secs), _ => None, } }); diff --git a/crates/cache/src/config/tests.rs b/crates/cache/src/config/tests.rs index a605c9e216b7..c88ae84c870a 100644 --- a/crates/cache/src/config/tests.rs +++ b/crates/cache/src/config/tests.rs @@ -400,6 +400,27 @@ fn test_duration_settings() { ); } +#[test] +fn test_duration_overflow() { + let (_td, cd, cp) = test_prolog(); + + for (num, unit) in [ + (u64::MAX / 60 + 1, "m"), + (u64::MAX / (60 * 60) + 1, "h"), + (u64::MAX / (60 * 60 * 24) + 1, "d"), + ] { + let content = format!( + "[cache]\ndirectory = '{}'\ncleanup-interval = '{num}{unit}'", + cd.display() + ); + fs::write(&cp, content).expect("Failed to write test config file"); + assert!( + CacheConfig::from_file(Some(&cp)).is_err(), + "duration {num}{unit} should overflow" + ); + } +} + #[test] fn test_percent_settings() { let (_td, cd, cp) = test_prolog();