Skip to content

Commit 3855592

Browse files
authored
Prevent underflow with saturating_sub(1) (#812) (#817)
1 parent 176c3ba commit 3855592

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/dynamics/island_manager.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl IslandManager {
3737
}
3838

3939
pub(crate) fn num_islands(&self) -> usize {
40-
self.active_islands.len() - 1
40+
self.active_islands.len().saturating_sub(1)
4141
}
4242

4343
/// Update this data-structure after one or multiple rigid-bodies have been removed for `bodies`.
@@ -244,8 +244,8 @@ impl IslandManager {
244244
self.active_islands.clear();
245245
self.active_islands.push(0);
246246

247-
// The max avoid underflow when the stack is empty.
248-
let mut island_marker = self.stack.len().max(1) - 1;
247+
// saturating_sub(1) prevents underflow when the stack is empty.
248+
let mut island_marker = self.stack.len().saturating_sub(1);
249249

250250
// NOTE: islands containing a body with non-standard number of iterations won’t
251251
// be merged with another island, unless another island with standard

src/dynamics/solver/interaction_groups.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ impl ParallelInteractionGroups {
5858
let range = self.groups[i]..self.groups[i + 1];
5959
&self.sorted_interactions[range]
6060
}
61+
6162
pub fn num_groups(&self) -> usize {
62-
self.groups.len() - 1
63+
self.groups.len().saturating_sub(1)
6364
}
6465

6566
pub fn group_interactions<Interaction: PairInteraction>(

0 commit comments

Comments
 (0)