-
Notifications
You must be signed in to change notification settings - Fork 162
fix: correct underflow in random_updated_at
#4363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,9 +197,16 @@ impl Cache { | |
| /// in the past, to avoid spikes of expired prices all being fetched at | ||
| /// once. | ||
| fn random_updated_at(max_age: Duration, now: Instant, rng: &mut impl Rng) -> Instant { | ||
| let percent_expired = rng.random_range(50..=90); | ||
| let age = max_age.as_secs() * percent_expired / 100; | ||
| now - Duration::from_secs(age) | ||
| let percent_expired: u32 = rng.random_range(50..=90); | ||
|
|
||
| let age = max_age | ||
| .checked_mul(percent_expired) | ||
| .map(|age| age / 100) | ||
| .unwrap_or(Duration::MAX); | ||
|
|
||
| now.checked_sub(age) | ||
| .or_else(|| now.checked_sub(Duration::from_secs(1))) | ||
| .unwrap_or(now) | ||
|
Comment on lines
+207
to
+209
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use saturating_sub instead
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not available for an Instant. |
||
| } | ||
|
|
||
| fn len(&self) -> usize { | ||
|
|
@@ -1143,4 +1150,34 @@ mod tests { | |
| anyhow!("protocol") | ||
| )))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn random_updated_at_underflow_check() { | ||
| let now = Instant::now(); | ||
| let max_age = Duration::MAX; | ||
| let mut rng = rand::rng(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seed the rng for determinism |
||
|
|
||
| let updated_at = Cache::random_updated_at(max_age, now, &mut rng); | ||
|
|
||
| assert!(updated_at < now); | ||
| assert!(updated_at >= now - Duration::from_secs(1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn random_updated_at_range() { | ||
| let now = Instant::now(); | ||
| let max_age = Duration::from_secs(600); | ||
| let mut rng = rand::rng(); | ||
|
|
||
| for _ in 0..100 { | ||
| let updated_at = Cache::random_updated_at(max_age, now, &mut rng); | ||
|
|
||
| let min_age = max_age * 50 / 100; | ||
| let max_age_check = max_age * 90 / 100; | ||
|
|
||
| assert!(updated_at < now); | ||
| assert!(updated_at <= now - min_age); | ||
| assert!(updated_at >= now - max_age_check); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use saturating_mul instead