diff options
author | Simon Ochsenreither <simon@ochsenreither.de> | 2013-12-18 00:34:00 +0100 |
---|---|---|
committer | Simon Ochsenreither <simon@ochsenreither.de> | 2013-12-23 10:50:57 +0100 |
commit | d2ee92f05561d2f58f6353856ccbf6467f9b23d9 (patch) | |
tree | 6944b2cc65cdce120c0943f8e99898c5540d4cd6 /src | |
parent | b5ef79f2f8bc010220d2920a890352d96ad84b45 (diff) | |
download | scala-d2ee92f05561d2f58f6353856ccbf6467f9b23d9.tar.gz scala-d2ee92f05561d2f58f6353856ccbf6467f9b23d9.tar.bz2 scala-d2ee92f05561d2f58f6353856ccbf6467f9b23d9.zip |
SI-7880 Fix infinite loop in ResizableArray#ensureSize
This issue was triggered for values greater than Int.MaxValue/2, which
caused the computation of the new array size to overflow and become
negative, leading to an infinite loop.
Diffstat (limited to 'src')
-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 } } |