Skip to content

Commit d796369

Browse files
authored
Fix casts in overflow checking function (#2024)
1 parent 72c4066 commit d796369

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

utbot-framework-test/src/test/kotlin/org/utbot/examples/math/OverflowAsErrorTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ internal class OverflowAsErrorTest : UtValueTestCaseChecker(
6969
}
7070
}
7171

72+
@Test
73+
fun testByteWithIntOverflow() {
74+
withTreatingOverflowAsError {
75+
checkWithException(
76+
OverflowExamples::byteWithIntOverflow,
77+
eq(2),
78+
{ x, y, r ->
79+
runCatching {
80+
Math.addExact(x.toInt(), y)
81+
}.isFailure && r.isException<OverflowDetectionError>()
82+
},
83+
{ x, y, r -> Math.addExact(x.toInt(), y).toByte() == r.getOrThrow() }
84+
)
85+
}
86+
}
87+
7288
@Test
7389
fun testByteSubOverflow() {
7490
withTreatingOverflowAsError {

utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,21 +3780,40 @@ class Traverser(
37803780
private fun TraversalContext.intOverflowCheck(op: BinopExpr, leftRaw: PrimitiveValue, rightRaw: PrimitiveValue) {
37813781
// cast to the bigger type
37823782
val sort = simpleMaxSort(leftRaw, rightRaw) as UtPrimitiveSort
3783-
val left = leftRaw.expr.toPrimitiveValue(sort.type)
3784-
val right = rightRaw.expr.toPrimitiveValue(sort.type)
3783+
val left = UtCastExpression(leftRaw, sort.type)
3784+
val right = UtCastExpression(rightRaw, sort.type)
3785+
3786+
val leftPrimValue = left.toPrimitiveValue(left.type)
3787+
val rightPrimValue = right.toPrimitiveValue(right.type)
37853788

37863789
val overflow = when (op) {
3787-
is JAddExpr -> {
3788-
mkNot(UtAddNoOverflowExpression(left.expr, right.expr))
3789-
}
3790-
is JSubExpr -> {
3791-
mkNot(UtSubNoOverflowExpression(left.expr, right.expr))
3792-
}
3790+
is JAddExpr -> mkNot(UtAddNoOverflowExpression(left, right))
3791+
is JSubExpr -> mkNot(UtSubNoOverflowExpression(left, right))
37933792
is JMulExpr -> when (sort.type) {
3794-
is ByteType -> lowerIntMulOverflowCheck(left, right, Byte.MIN_VALUE.toInt(), Byte.MAX_VALUE.toInt())
3795-
is ShortType -> lowerIntMulOverflowCheck(left, right, Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt())
3796-
is IntType -> higherIntMulOverflowCheck(left, right, Int.SIZE_BITS, Int.MIN_VALUE.toLong()) { it: UtExpression -> it.toIntValue() }
3797-
is LongType -> higherIntMulOverflowCheck(left, right, Long.SIZE_BITS, Long.MIN_VALUE) { it: UtExpression -> it.toLongValue() }
3793+
is ByteType -> lowerIntMulOverflowCheck(
3794+
leftPrimValue,
3795+
rightPrimValue,
3796+
Byte.MIN_VALUE.toInt(),
3797+
Byte.MAX_VALUE.toInt()
3798+
)
3799+
is ShortType -> lowerIntMulOverflowCheck(
3800+
leftPrimValue,
3801+
rightPrimValue,
3802+
Short.MIN_VALUE.toInt(),
3803+
Short.MAX_VALUE.toInt()
3804+
)
3805+
is IntType -> higherIntMulOverflowCheck(
3806+
leftPrimValue,
3807+
rightPrimValue,
3808+
Int.SIZE_BITS,
3809+
Int.MIN_VALUE.toLong()
3810+
) { it: UtExpression -> it.toIntValue() }
3811+
is LongType -> higherIntMulOverflowCheck(
3812+
leftPrimValue,
3813+
rightPrimValue,
3814+
Long.SIZE_BITS,
3815+
Long.MIN_VALUE
3816+
) { it: UtExpression -> it.toLongValue() }
37983817
else -> null
37993818
}
38003819
else -> null

utbot-sample/src/main/java/org/utbot/examples/math/OverflowExamples.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ public class OverflowExamples {
44
public byte byteAddOverflow(byte x, byte y) {
55
return (byte) (x + y);
66
}
7+
public byte byteWithIntOverflow(byte x, int y) {
8+
return (byte) (x + y);
9+
}
710
public byte byteMulOverflow(byte x, byte y) {
811
return (byte) (x * y);
912
}

0 commit comments

Comments
 (0)