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
@@ -143,6 +143,7 @@ actual class ArrayList<E> private constructor(
final actual fun ensureCapacity(minCapacity: Int) {
if (backing != null) throw IllegalStateException() // just in case somebody casts subList to ArrayList
if (minCapacity < 0) throw OutOfMemoryError() // overflow
if (minCapacity > array.size) {
val newSize = ArrayDeque.newCapacity(array.size, minCapacity)
array = array.copyOfUninitializedElements(newSize)
@@ -180,6 +180,7 @@ actual class HashMap<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