👺 Havoc: Prevent panic on jittered_ttl overflow#1807
Conversation
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Code Review
This pull request prevents potential panics in jittered_ttl due to overflow when multiplying a large Duration by a random jitter factor, and adds a property-based test to verify this safety. The feedback suggests using Duration::try_from_secs_f64 to perform a cleaner, safer, and more idiomatic checked conversion instead of manually checking against u64::MAX as f64 and suppressing the clippy::cast_precision_loss lint.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| #[allow(clippy::cast_precision_loss)] | ||
| let max_secs = u64::MAX as f64; | ||
| if base.as_secs_f64() * factor >= max_secs { | ||
| Duration::MAX | ||
| } else { | ||
| base.mul_f64(factor) | ||
| } |
There was a problem hiding this comment.
Instead of manually checking for overflow against u64::MAX as f64 (which requires suppressing the clippy::cast_precision_loss lint), you can use Duration::try_from_secs_f64 directly. This is cleaner, safer, and idiomatic in Rust for performing checked float-to-Duration conversions.
Duration::try_from_secs_f64(base.as_secs_f64() * factor).unwrap_or(Duration::MAX)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## trunk-dev #1807 +/- ##
=============================================
- Coverage 86.82% 86.82% -0.01%
=============================================
Files 273 273
Lines 202424 202427 +3
=============================================
Hits 175761 175761
- Misses 26663 26666 +3 🚀 New features to boost your workflow:
|
🧊 The Trigger:
Passing a very large
Duration(e.g.Duration::MAX) tojittered_ttlwith a fraction that results in a randomized jitter factor> 1.0.📉 The Stack Trace:
🧪 Reproduction:
Run the new proptest harness located at
autumn/tests/integration/chaos_cache_jitter_proptest.rs. The fuzzer hits this edge case by feeding extreme ranges intojittered_ttl.😈 Comment:
"You assumed
Duration::mul_f64was infallible and would never overflow when multiplying large durations by random jitter. You were wrong."PR created automatically by Jules for task 257098097593059716 started by @madmax983