From cf7b51db3b289d2b1782ffb863912217936dcccb 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 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index d7dc202fad..cb7d2095bc 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -1109,12 +1109,14 @@ 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 } + // TODO: return i - start so the caller knows how many values read? } /** Tests if another iterator produces the same values as this one. -- cgit v1.2.3