rng ranges: Use gen_range_infallible() for Duration::ZERO..=T
This commit is contained in:
parent
09f0ecaa71
commit
b151237a7f
|
@ -330,6 +330,7 @@ dependencies = [
|
|||
"rand 0.8.5",
|
||||
"serde",
|
||||
"tokio",
|
||||
"tor-basic-utils",
|
||||
"tor-checkable",
|
||||
"tor-config",
|
||||
"tor-dirmgr",
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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<R: Runtime> TcpProvider for BrokenTcpProvider<R> {
|
|||
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")))
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<R: Runtime> DirState for GetMicrodescsState<R> {
|
|||
/// 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
|
||||
|
|
|
@ -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<R: Rng>(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)
|
||||
|
|
Loading…
Reference in New Issue