From 92cf0e354e38d450bfe34d4612f9f869faec2baf Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Mon, 17 Dec 2012 23:28:08 -0500 Subject: Fix Iterator#copyToArray (fixes SI-6827). As pointed out in #scala, when using a non-zero start it's possible to get an ArrayIndexOutOfBoundsException due to an incorrect bounds check. This patch fixes this, as well as another potential bounds error, and adds test cases. Incorporates some other suggestions by Som-Snytt to ensure that callers will get useful error messages in cases where the start parameter is wrong (negative or out-of-array-bounds). Review by @som-snytt. --- src/library/scala/collection/Iterator.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index cbf8cc4931..696bc4ab5c 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -1111,9 +1111,10 @@ trait Iterator[+A] extends TraversableOnce[A] { * $willNotTerminateInf */ def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Unit = { + require(start >= 0 && start < xs.length, s"start $start out of range ${xs.length}") var i = start - val end = start + math.min(len, xs.length) - while (hasNext && i < end) { + val end = start + math.min(len, xs.length - start) + while (i < end && hasNext) { xs(i) = next() i += 1 } -- cgit v1.2.3