Skip to content

Commit

Permalink
Merge PR #292: Fix overflow bug in DividePrim
Browse files Browse the repository at this point in the history
  • Loading branch information
smarr committed Aug 5, 2019
2 parents d3985a8 + cea6675 commit eb16019
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core-lib/TestSuite/IntegerTests.ns
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ class IntegerTests usingPlatform: platform testFramework: minitest = (

self assert: 3 equals: 7 / 2.0.
self assert: 2 equals: 7 / 3.5.

self assert: 9223372036854775808 equals: (* Java.MIN_VALUE *) -9223372036854775808 / -1.

(* To trigger issue, we need to compute the value, because it is otherwise parsed as a big integer *)
self assert: 9223372036854775808 equals: (* Java.MIN_VALUE *) ((-922337203685477580 * 10) - 8) / -1.
)

public testDouble = (
Expand Down
14 changes: 13 additions & 1 deletion src/som/primitives/arithmetic/DividePrim.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
@GenerateNodeFactory
@Primitive(primitive = "int:divideBy:", selector = "/")
public abstract class DividePrim extends ArithmeticPrim {
@Specialization
private static final BigInteger OVERFLOW_RESULT =
BigInteger.valueOf(Long.MIN_VALUE).divide(BigInteger.valueOf(-1));

@Specialization(guards = "!isOverflowDivision(left, right)")
public final long doLong(final long left, final long right) {
return left / right;
}

@Specialization(guards = "isOverflowDivision(left, right)")
public final Object doLongWithOverflow(final long left, final long right) {
return OVERFLOW_RESULT;
}

@Specialization
@TruffleBoundary
public final Object doBigInteger(final BigInteger left, final BigInteger right) {
Expand All @@ -40,4 +48,8 @@ public final Object doLong(final long left, final BigInteger right) {
public final Object doLong(final long left, final double right) {
return (long) (left / right);
}

protected static final boolean isOverflowDivision(final long left, final long right) {
return left == Long.MIN_VALUE && right == -1;
}
}

0 comments on commit eb16019

Please sign in to comment.