diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2014-01-03 02:35:08 -0800 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2014-01-03 02:35:08 -0800 |
commit | 66a12fc264cae53107fda4feb5e42fda6879c1ae (patch) | |
tree | 756db8baa6be5aa45b32d2bec6882e7221b9e927 /src/library | |
parent | 30e0bcf5f3158d532ebf874e2e38393cae34658c (diff) | |
parent | d2ee92f05561d2f58f6353856ccbf6467f9b23d9 (diff) | |
download | scala-66a12fc264cae53107fda4feb5e42fda6879c1ae.tar.gz scala-66a12fc264cae53107fda4feb5e42fda6879c1ae.tar.bz2 scala-66a12fc264cae53107fda4feb5e42fda6879c1ae.zip |
Merge pull request #3284 from soc/SI-7880
SI-7880 Fix infinite loop in ResizableArray#ensureSize
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/collection/mutable/ResizableArray.scala | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/library/scala/collection/mutable/ResizableArray.scala b/src/library/scala/collection/mutable/ResizableArray.scala index 4a12f9588c..c3047522e2 100644 --- a/src/library/scala/collection/mutable/ResizableArray.scala +++ b/src/library/scala/collection/mutable/ResizableArray.scala @@ -89,16 +89,20 @@ trait ResizableArray[A] extends IndexedSeq[A] } } - /** Ensure that the internal array has at `n` cells. */ + /** Ensure that the internal array has at least `n` cells. */ protected def ensureSize(n: Int) { - if (n > array.length) { - var newsize = array.length * 2 - while (n > newsize) - newsize = newsize * 2 - - val newar: Array[AnyRef] = new Array(newsize) - scala.compat.Platform.arraycopy(array, 0, newar, 0, size0) - array = newar + // Use a Long to prevent overflows + val arrayLength: Long = array.length + if (n > arrayLength) { + var newSize: Long = arrayLength * 2 + while (n > newSize) + newSize = newSize * 2 + // Clamp newSize to Int.MaxValue + if (newSize > Int.MaxValue) newSize = Int.MaxValue + + val newArray: Array[AnyRef] = new Array(newSize.toInt) + scala.compat.Platform.arraycopy(array, 0, newArray, 0, size0) + array = newArray } } |