KT-31639: Fix Iterables.drop integer overflow

This commit is contained in:
Eduard Wolf
2019-05-27 19:16:13 +02:00
committed by Ilya Gorbunov
parent e5565b817b
commit c885cadde4
3 changed files with 8 additions and 2 deletions
@@ -614,7 +614,7 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
} }
var count = 0 var count = 0
for (item in this) { for (item in this) {
if (count++ >= n) list.add(item) if (count >= n) list.add(item) else ++count
} }
return list.optimizeReadOnlyList() return list.optimizeReadOnlyList()
} }
@@ -156,4 +156,10 @@ class IndexOverflowJVMTest {
assertEquals(expectedEnd, dropTake.last()) assertEquals(expectedEnd, dropTake.last())
} }
@Test
fun dropMaxValue() {
val range = 0L..Int.MAX_VALUE + 1L
assertEquals(listOf(Int.MAX_VALUE.toLong(), Int.MAX_VALUE + 1L), range.drop(Int.MAX_VALUE))
}
} }
@@ -84,7 +84,7 @@ object Filtering : TemplateGroupBase() {
} }
var count = 0 var count = 0
for (item in this) { for (item in this) {
if (count++ >= n) list.add(item) if (count >= n) list.add(item) else ++count
} }
return list.optimizeReadOnlyList() return list.optimizeReadOnlyList()
""" """