Skip to content

Commit fd71bef

Browse files
committed
prevent overflow in size_hint for large number of permutations in QueryPermutationIter
1 parent 3503fbc commit fd71bef

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

crates/bevy_ecs/src/query/iter.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,19 @@ where
187187
.map(|index| self.world.archetypes[ArchetypeId::new(index)].len())
188188
.sum();
189189

190-
// n! / k!(n-k)! = (n*n-1*...*n-k+1) / k!
191-
let k_factorial: usize = (1..=K).product();
192-
let max_permutations =
193-
(0..K).fold(1, |n, i| n * (max_size.saturating_sub(i))) / k_factorial;
190+
if max_size < K {
191+
return (0, Some(0));
192+
}
194193

195-
(0, Some(max_permutations))
194+
// n! / k!(n-k)! = (n*n-1*...*n-k+1) / k!
195+
let max_permutations = (0..K)
196+
.try_fold(1usize, |n, i| n.checked_mul(max_size - i))
197+
.map(|n| {
198+
let k_factorial: usize = (1..=K).product();
199+
n / k_factorial
200+
});
201+
202+
(0, max_permutations)
196203
}
197204
}
198205

0 commit comments

Comments
 (0)