diff --git a/crates/tor-circmgr/src/hspool/pool.rs b/crates/tor-circmgr/src/hspool/pool.rs index ec60fafe1..63077ac42 100644 --- a/crates/tor-circmgr/src/hspool/pool.rs +++ b/crates/tor-circmgr/src/hspool/pool.rs @@ -144,6 +144,9 @@ where P: Fn(&T) -> bool, { let n_circuits = slice.len(); + if n_circuits == 0 { + return None; + } let shift = rng.gen_range(0..n_circuits); (shift..n_circuits) .chain(0..shift) @@ -183,4 +186,11 @@ mod test { assert!(found[idx] == (num & 1 == 1)); } } + + #[test] + fn random_idx_empty() { + let mut rng = testing_rng(); + let idx = random_idx_where(&mut rng, &[], |_: &i32| panic!()); + assert_eq!(idx, None); + } }