From b151237a7f8651ed8694748832af05786a7eccfd Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 7 Jul 2023 14:19:55 +0100 Subject: [PATCH] rng ranges: Use gen_range_infallible() for Duration::ZERO..=T --- Cargo.lock | 1 + crates/arti-testing/Cargo.toml | 1 + crates/arti-testing/src/rt/badtcp.rs | 5 +++-- crates/tor-circmgr/src/timeouts/pareto.rs | 2 +- crates/tor-dirmgr/src/state.rs | 5 ++--- crates/tor-guardmgr/src/util.rs | 3 ++- 6 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e23bff21..99e3eb44d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -330,6 +330,7 @@ dependencies = [ "rand 0.8.5", "serde", "tokio", + "tor-basic-utils", "tor-checkable", "tor-config", "tor-dirmgr", diff --git a/crates/arti-testing/Cargo.toml b/crates/arti-testing/Cargo.toml index 828f94fdc..3d9c4bf01 100644 --- a/crates/arti-testing/Cargo.toml +++ b/crates/arti-testing/Cargo.toml @@ -37,6 +37,7 @@ pin-project = "1" rand = "0.8" serde = { version = "1.0.103", features = ["derive"] } tokio = { version = "1.7", features = ["signal", "macros"] } +tor-basic-utils = { path = "../tor-basic-utils", version = "0.7.1" } tor-checkable = { path = "../tor-checkable", version = "0.5.1", features = ["experimental-api"] } tor-config = { path = "../tor-config", version = "0.9.2" } tor-dirmgr = { package = "tor-dirmgr", path = "../tor-dirmgr", version = "0.10.2", features = ["dirfilter"] } diff --git a/crates/arti-testing/src/rt/badtcp.rs b/crates/arti-testing/src/rt/badtcp.rs index f950275aa..34258dfbb 100644 --- a/crates/arti-testing/src/rt/badtcp.rs +++ b/crates/arti-testing/src/rt/badtcp.rs @@ -8,7 +8,7 @@ use anyhow::anyhow; use async_trait::async_trait; use futures::io::{AsyncRead, AsyncWrite}; use pin_project::pin_project; -use rand::{thread_rng, Rng}; +use rand::thread_rng; use std::io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult}; use std::net::SocketAddr; use std::pin::Pin; @@ -16,6 +16,7 @@ use std::str::FromStr; use std::sync::{Arc, Mutex}; use std::task::{Context, Poll}; use std::time::Duration; +use tor_basic_utils::RngExt as _; /// An action that we can take upon trying to make a TCP connection. #[derive(Debug, Copy, Clone)] @@ -154,7 +155,7 @@ impl TcpProvider for BrokenTcpProvider { Ok(BreakableTcpStream::Present(conn)) } Action::Fail(dur, kind) => { - let d = thread_rng().gen_range(Duration::from_secs(0)..=dur); + let d = thread_rng().gen_range_infallible(..=dur); self.inner.sleep(d).await; Err(IoError::new(kind, anyhow::anyhow!("intentional failure"))) } diff --git a/crates/tor-circmgr/src/timeouts/pareto.rs b/crates/tor-circmgr/src/timeouts/pareto.rs index 90a6d1b50..f654dc516 100644 --- a/crates/tor-circmgr/src/timeouts/pareto.rs +++ b/crates/tor-circmgr/src/timeouts/pareto.rs @@ -657,7 +657,7 @@ mod test { use super::*; use crate::timeouts::TimeoutEstimator; use tor_basic_utils::test_rng::testing_rng; - use tor_basic_utils::{RngExt as _}; + use tor_basic_utils::RngExt as _; /// Return an action to build a 3-hop circuit. fn b3() -> Action { diff --git a/crates/tor-dirmgr/src/state.rs b/crates/tor-dirmgr/src/state.rs index 7d83a59af..862374507 100644 --- a/crates/tor-dirmgr/src/state.rs +++ b/crates/tor-dirmgr/src/state.rs @@ -10,13 +10,13 @@ //! [`bootstrap`](crate::bootstrap) module for functions that actually //! load or download directory information. -use rand::Rng; use std::collections::{HashMap, HashSet}; use std::fmt::Debug; use std::mem; use std::sync::{Arc, Mutex}; use std::time::{Duration, SystemTime}; use time::OffsetDateTime; +use tor_basic_utils::RngExt as _; use tor_error::{internal, warn_report}; use tor_netdir::{MdReceiver, NetDir, PartialNetDir}; use tor_netdoc::doc::authcert::UncheckedAuthCert; @@ -1151,8 +1151,7 @@ impl DirState for GetMicrodescsState { /// is `lifetime`. fn pick_download_time(lifetime: &Lifetime) -> SystemTime { let (lowbound, uncertainty) = client_download_range(lifetime); - let zero = Duration::new(0, 0); - lowbound + rand::thread_rng().gen_range(zero..=uncertainty) + lowbound + rand::thread_rng().gen_range_infallible(..=uncertainty) } /// Based on the lifetime for a consensus, return the time range during which diff --git a/crates/tor-guardmgr/src/util.rs b/crates/tor-guardmgr/src/util.rs index 75966e4d0..9e846dc7f 100644 --- a/crates/tor-guardmgr/src/util.rs +++ b/crates/tor-guardmgr/src/util.rs @@ -2,6 +2,7 @@ use rand::Rng; use std::time::{Duration, SystemTime}; +use tor_basic_utils::RngExt as _; /// Return a random time within the range `when-max ..= when`. /// @@ -12,7 +13,7 @@ use std::time::{Duration, SystemTime}; /// an attempt to make some kinds of traffic analysis attacks a bit /// harder for an attacker who can read our state after the fact. pub(crate) fn randomize_time(rng: &mut R, when: SystemTime, max: Duration) -> SystemTime { - let offset = rng.gen_range(Duration::ZERO..=max); + let offset = rng.gen_range_infallible(..=max); let random = when .checked_sub(offset) .unwrap_or(SystemTime::UNIX_EPOCH)