Do not leak primitiveness of an array wrapped with asList
A primitive array wrapped in a List with asList had incorrect implementation of toArray method: while it declares that an object array is returned, it returned a primitive array. Therefore the methods such as `Collection.toTypedArray()` and its dependents `ArrayList(collection)`, `Collection + Iterable` might behave incorrectly having relied on `toTypedArray` returned an object array. #KT-21828 Fixed
This commit is contained in:
@@ -117,7 +117,7 @@ public open class ArrayList<E> internal constructor(private var array: Array<Any
|
||||
override fun lastIndexOf(element: E): Int = array.lastIndexOf(element)
|
||||
|
||||
override fun toString() = arrayToString(array)
|
||||
override fun toArray(): Array<Any?> = array.copyOf()
|
||||
override fun toArray(): Array<Any?> = js("[]").slice.call(array)
|
||||
|
||||
|
||||
private fun rangeCheck(index: Int) = index.apply {
|
||||
|
||||
@@ -36,6 +36,13 @@ class JsCollectionsTest {
|
||||
snapshotDoesNotCreateView(arrayOf<Any>("first", "last"), { arrayListOf(*it) })
|
||||
}
|
||||
|
||||
@Suppress("USELESS_CAST")
|
||||
@Test fun asListHidesPrimitivenessOfArray() {
|
||||
assertTrue(intArrayOf(1).asList().toTypedArray() as Any is Array<*>, "IntArray primitiveness leaks")
|
||||
assertTrue(longArrayOf(1).asList().toTypedArray() as Any is Array<*>, "LongArray primitiveness leaks")
|
||||
assertTrue(charArrayOf(' ').asList().toTypedArray() as Any is Array<*>, "CharArray primitiveness leaks")
|
||||
}
|
||||
|
||||
@Test fun arrayListCapacity() {
|
||||
val list = ArrayList<Any>(20)
|
||||
list.ensureCapacity(100)
|
||||
|
||||
Reference in New Issue
Block a user