From c3619101731f45cce0464247e6ed43170beac220 Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Tue, 14 Dec 2021 15:55:36 +0100 Subject: [PATCH] OK, this can be checked once --- src/Tokenizer/Tokens.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 675308aac64..15044fef033 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -858,6 +858,13 @@ public function insertSlices(array $slices): void $this->setSize($oldSize + $itemsCount); krsort($slices); + $farthestSliceIndex = key($slices); + + // We check only the farthest index, if it's within the size of collection, other indices will be valid too. + if (!\is_int($farthestSliceIndex) || $farthestSliceIndex > $oldSize) { + throw new \OutOfBoundsException(sprintf('Cannot insert index "%s" outside of collection.', $farthestSliceIndex)); + } + $previousSliceIndex = $oldSize; // since we only move already existing items around, we directly call into SplFixedArray::offset* methods. @@ -867,10 +874,6 @@ public function insertSlices(array $slices): void throw new \OutOfBoundsException(sprintf('Invalid index "%s".', $index)); } - if ($index > $oldSize) { - throw new \OutOfBoundsException(sprintf('Cannot insert index "%s" outside of collection.', $index)); - } - $slice = \is_array($slice) || $slice instanceof self ? $slice : [$slice]; $sliceCount = \count($slice);