Skip to content

Commit 3351aaa

Browse files
committed
Fix bug with overflow checking in Combinations
1 parent d21426f commit 3351aaa

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/Combinations.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ class Combinations(vararg elementNumbers: Int): Iterable<IntArray> {
7474
}
7575
count = LongArray(elementNumbers.size) { elementNumbers[it].toLong() }
7676
for (i in count.size - 2 downTo 0) {
77-
count[i] = count[i] * count[i + 1]
78-
if(count[i] < count[i + 1]) {
79-
throw TooManyCombinationsException("Long overflow or bad sequence: ${count[i]} < ${count[i + 1]}")
77+
try {
78+
count[i] = StrictMath.multiplyExact(count[i], count[i + 1])
79+
} catch (e: ArithmeticException) {
80+
throw TooManyCombinationsException("Long overflow: ${count[i]} * ${count[i + 1]}")
8081
}
8182
}
8283
}

utbot-fuzzers/src/test/kotlin/org/utbot/framework/plugin/api/CombinationsTest.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,24 @@ class CombinationsTest {
228228
assertTrue(expected.isEmpty())
229229
}
230230

231+
@Test
232+
fun testCartesianProductDoesNotThrowsExceptionBeforeOverflow() {
233+
// We assume that a standard method has no more than 7 parameters.
234+
// In this case every parameter can accept up to 511 values without Long overflow.
235+
// CartesianProduct throws exception
236+
val values = Array(511) { it }.toList()
237+
val parameters = Array(7) { values }.toList()
238+
assertDoesNotThrow {
239+
CartesianProduct(parameters, Random(0)).asSequence()
240+
}
241+
}
242+
231243
@Test
232244
fun testCartesianProductThrowsExceptionOnOverflow() {
233245
// We assume that a standard method has no more than 7 parameters.
234-
// In this case every parameter can accept up to 1700 values without Long overflow.
246+
// In this case every parameter can accept up to 511 values without Long overflow.
235247
// CartesianProduct throws exception
236-
val values = Array(1701) { it }.toList()
248+
val values = Array(512) { it }.toList()
237249
val parameters = Array(7) { values }.toList()
238250
assertThrows(TooManyCombinationsException::class.java) {
239251
CartesianProduct(parameters, Random(0)).asSequence()

utbot-fuzzers/src/test/kotlin/org/utbot/framework/plugin/api/FuzzerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ class FuzzerTest {
115115
), fuzz.map { arguments -> arguments.map { fuzzedValue -> fuzzedValue.model } }.toSet())
116116
}
117117

118-
// Because of Long limitation fuzzer can process no more than 1700 values for method with 7 parameters
118+
// Because of Long limitation fuzzer can process no more than 511 values for method with 7 parameters
119119
@Test
120120
@Timeout(1, unit = TimeUnit.SECONDS)
121121
fun `the worst case works well`() {
122122
assertDoesNotThrow {
123-
val values = (0 until 1700).map { UtPrimitiveModel(it).fuzzed() }.asSequence()
123+
val values = (0 until 511).map { UtPrimitiveModel(it).fuzzed() }.asSequence()
124124
val provider = ModelProvider { descr ->
125125
(0 until descr.parameters.size).asSequence()
126126
.flatMap { index -> values.map { FuzzedParameter(index, it) } }

0 commit comments

Comments
 (0)