diff options
author | Erik Osheim <d_m@plastic-idolatry.com> | 2012-12-17 23:28:08 -0500 |
---|---|---|
committer | Erik Osheim <d_m@plastic-idolatry.com> | 2012-12-20 11:24:15 -0500 |
commit | cf7b51db3b289d2b1782ffb863912217936dcccb (patch) | |
tree | fb59d53d6eb8376a1f1be0c520ff8f9380ff2234 /src/library/scala/collection/Iterator.scala | |
parent | e14917528e1c080a7f10785e21de36f3a7769718 (diff) | |
download | scala-cf7b51db3b289d2b1782ffb863912217936dcccb.tar.gz scala-cf7b51db3b289d2b1782ffb863912217936dcccb.tar.bz2 scala-cf7b51db3b289d2b1782ffb863912217936dcccb.zip |
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.
Diffstat (limited to 'src/library/scala/collection/Iterator.scala')
-rw-r--r-- | src/library/scala/collection/Iterator.scala | 6 |
1 files changed, 4 insertions, 2 deletions
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. |