Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10448 from dragonfly-ai/2.13.x
Prevent ArrayBuilder capacity overflow/infinite looping in ArrayBuilder.ensureSize(size:Int).
- Loading branch information
Showing
7 changed files
with
99 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/junit/scala/collection/mutable/ArrayBuilderTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package scala.collection.mutable | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
import scala.runtime.PStatics.VM_MaxArraySize | ||
import scala.tools.testkit.AssertUtil.assertThrows | ||
|
||
class ArrayBuilderTest { | ||
@Test | ||
def t12617: Unit = { | ||
val ab: ArrayBuilder[Unit] = ArrayBuilder.make[Unit] | ||
|
||
// ArrayBuilder.ofUnit.addAll doesn't iterate if the iterator has a `knownSize` | ||
ab.addAll(new Iterator[Unit] { | ||
override def knownSize: Int = VM_MaxArraySize | ||
def hasNext: Boolean = true | ||
def next(): Unit = () | ||
}) | ||
|
||
// reached maximum size without entering an infinite loop? | ||
assertEquals(ab.length, VM_MaxArraySize) | ||
|
||
// expect an exception when trying to grow larger than maximum size by addOne | ||
assertThrows[Exception](ab.addOne(()), _.endsWith("Requested length: 2147483640; current length: 2147483639")) | ||
|
||
val arr = Array[Unit]((), (), (), (), (), (), (), (), (), (), (), ()) | ||
|
||
// expect an exception when trying to grow larger than maximum size by addAll(iterator) | ||
assertThrows[Exception](ab.addAll(arr.iterator), _.endsWith("Requested length: -2147483645; current length: 2147483639; increase: 12")) | ||
|
||
// expect an exception when trying to grow larger than maximum size by addAll(array) | ||
assertThrows[Exception](ab.addAll(arr)) | ||
} | ||
} |