summaryrefslogtreecommitdiff
path: root/test/files/run/t6827.scala
diff options
context:
space:
mode:
authorErik Osheim <d_m@plastic-idolatry.com>2012-12-17 23:28:08 -0500
committerAdriaan Moors <adriaan.moors@typesafe.com>2013-01-04 11:48:38 -0800
commit92cf0e354e38d450bfe34d4612f9f869faec2baf (patch)
tree989fc06d6b2c8e372e17e6ebefbba32770ae789c /test/files/run/t6827.scala
parente112ac94d04be7ee715ebf8724709123b0f3e1f6 (diff)
downloadscala-92cf0e354e38d450bfe34d4612f9f869faec2baf.tar.gz
scala-92cf0e354e38d450bfe34d4612f9f869faec2baf.tar.bz2
scala-92cf0e354e38d450bfe34d4612f9f869faec2baf.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 'test/files/run/t6827.scala')
-rw-r--r--test/files/run/t6827.scala31
1 files changed, 31 insertions, 0 deletions
diff --git a/test/files/run/t6827.scala b/test/files/run/t6827.scala
new file mode 100644
index 0000000000..7e8918e3dc
--- /dev/null
+++ b/test/files/run/t6827.scala
@@ -0,0 +1,31 @@
+object Test extends App {
+ val ns = (0 until 20)
+ val arr = new Array[Int](10)
+
+ def tryit(label: String, start: Int, len: Int): Unit = {
+ val status = try {
+ val it = ns.toIterator
+ it.copyToArray(arr, start, len)
+ "ok"
+ } catch {
+ case e: Exception => e.toString
+ }
+ println("%s: %s" format (label, status))
+ }
+
+ tryit("start at -5", -5, 10)
+ tryit("start at -1", -1, 10)
+ tryit("start at limit", 10, 10)
+ tryit("start at limit-1", 9, 10)
+ tryit("first 10", 0, 10)
+ tryit("read all", 0, 20)
+ tryit("test huge len", 0, Int.MaxValue)
+ tryit("5 from 5", 5, 10)
+ tryit("20 from 5", 5, 20)
+ tryit("test len overflow", 5, Int.MaxValue)
+ tryit("start beyond limit", 30, 10)
+ tryit("read 0", 0, 0)
+ tryit("read -1", 0, -1)
+ tryit("invalid read 0", 30, 0)
+ tryit("invalid read -1", 30, -1)
+}