Handle container builders capacity overflow

This commit is contained in:
Abduqodiri Qurbonzoda
2021-09-30 16:01:57 +00:00
committed by Space
parent f8bcba0b76
commit cca5f82aa0
6 changed files with 63 additions and 3 deletions
@@ -178,6 +178,7 @@ internal class ListBuilder<E> private constructor(
private fun ensureCapacity(minCapacity: Int) {
if (backing != null) throw IllegalStateException() // just in case somebody casts subList to ListBuilder
if (minCapacity < 0) throw OutOfMemoryError() // overflow
if (minCapacity > array.size) {
val newSize = ArrayDeque.newCapacity(array.size, minCapacity)
array = array.copyOfUninitializedElements(newSize)
@@ -177,6 +177,7 @@ internal class MapBuilder<K, V> private constructor(
}
private fun ensureCapacity(capacity: Int) {
if (capacity < 0) throw OutOfMemoryError() // overflow
if (capacity > this.capacity) {
var newSize = this.capacity * 3 / 2
if (capacity > newSize) newSize = capacity