From abebe6b49ea83b4f03656575aa7c2106440f1f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sat, 28 Jan 2023 18:58:49 +0100 Subject: [PATCH] Use `checked_sub` instead of an unchecked subtraction https://rust-lang.github.io/rust-clippy/master/index.html#unchecked_duration_subtraction --- acmed/src/endpoint.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/acmed/src/endpoint.rs b/acmed/src/endpoint.rs index d9ec144..a4016c1 100644 --- a/acmed/src/endpoint.rs +++ b/acmed/src/endpoint.rs @@ -101,23 +101,30 @@ impl RateLimit { fn request_allowed(&self) -> bool { for (max_allowed, duration) in self.limits.iter() { - let max_date = Instant::now() - *duration; - let nb_req = self - .query_log - .iter() - .filter(move |x| **x > max_date) - .count(); - if nb_req >= *max_allowed { - return false; - } + match Instant::now().checked_sub(*duration) { + Some(max_date) => { + let nb_req = self + .query_log + .iter() + .filter(move |x| **x > max_date) + .count(); + if nb_req >= *max_allowed { + return false; + } + } + None => { + return false; + } + }; } true } fn prune_log(&mut self) { if let Some((_, max_limit)) = self.limits.first() { - let prune_date = Instant::now() - *max_limit; - self.query_log.retain(move |&d| d > prune_date); + if let Some(prune_date) = Instant::now().checked_sub(*max_limit) { + self.query_log.retain(move |&d| d > prune_date); + } } } }