From 35beb9698e299ecaee3286e4ba9e9988a5ef595d Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 9 Jul 2018 12:42:58 +0300 Subject: [PATCH] Fix overflow in empty progressions' last element #KT-24204 Fixed --- .../src/kotlin/internal/progressionUtil.kt | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/core/builtins/src/kotlin/internal/progressionUtil.kt b/core/builtins/src/kotlin/internal/progressionUtil.kt index e6d158be49e..62b3724f83a 100644 --- a/core/builtins/src/kotlin/internal/progressionUtil.kt +++ b/core/builtins/src/kotlin/internal/progressionUtil.kt @@ -42,16 +42,10 @@ private fun differenceModulo(a: Long, b: Long, c: Long): Long { * @suppress */ @PublishedApi -internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int { - if (step > 0) { - return end - differenceModulo(end, start, step) - } - else if (step < 0) { - return end + differenceModulo(start, end, -step) - } - else { - throw kotlin.IllegalArgumentException("Step is zero.") - } +internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int = when { + step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step) + step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step) + else -> throw kotlin.IllegalArgumentException("Step is zero.") } /** @@ -71,14 +65,8 @@ internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int { * @suppress */ @PublishedApi -internal fun getProgressionLastElement(start: Long, end: Long, step: Long): Long { - if (step > 0) { - return end - differenceModulo(end, start, step) - } - else if (step < 0) { - return end + differenceModulo(start, end, -step) - } - else { - throw kotlin.IllegalArgumentException("Step is zero.") - } +internal fun getProgressionLastElement(start: Long, end: Long, step: Long): Long = when { + step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step) + step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step) + else -> throw kotlin.IllegalArgumentException("Step is zero.") }