From 5775f431f066f683a6c694eed76b6f16dfdd5c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Mon, 21 Apr 2025 22:38:43 +0200 Subject: [PATCH] don't overflow when relations are empty (#18891) # Objective - Fixes #18890 ## Solution - Don't overflow when substracting, bound at 0 ## Testing - Reproducer from the issue now works --- .../src/relationship/relationship_source_collection.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/relationship/relationship_source_collection.rs b/crates/bevy_ecs/src/relationship/relationship_source_collection.rs index 1a83c6c825..c2c9bd94d8 100644 --- a/crates/bevy_ecs/src/relationship/relationship_source_collection.rs +++ b/crates/bevy_ecs/src/relationship/relationship_source_collection.rs @@ -213,7 +213,7 @@ impl OrderedRelationshipSourceCollection for Vec { fn place_most_recent(&mut self, index: usize) { if let Some(entity) = self.pop() { - let index = index.min(self.len() - 1); + let index = index.min(self.len().saturating_sub(1)); self.insert(index, entity); } } @@ -221,7 +221,7 @@ impl OrderedRelationshipSourceCollection for Vec { fn place(&mut self, entity: Entity, index: usize) { if let Some(current) = <[Entity]>::iter(self).position(|e| *e == entity) { // The len is at least 1, so the subtraction is safe. - let index = index.min(self.len() - 1); + let index = index.min(self.len().saturating_sub(1)); Vec::remove(self, current); self.insert(index, entity); };