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
This commit is contained in:
François Mockers 2025-04-21 22:38:43 +02:00 committed by GitHub
parent 18e1bf1c3d
commit 12f71a8936
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -213,7 +213,7 @@ impl OrderedRelationshipSourceCollection for Vec<Entity> {
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<Entity> {
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);
};