Skip to content

Commit 84df344

Browse files
committed
[GR-15055] Fix: BigInteger multiplication with 0.
PullRequest: graalpython/480
2 parents c2d1579 + 80e6b03 commit 84df344

File tree

1 file changed

+13
-2
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints

1 file changed

+13
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,18 @@ PInt doLLOvf(long x, long y) {
649649
return factory().createInt(r);
650650
}
651651

652-
@Specialization
652+
@Specialization(guards = "right == 0")
653+
int doPIntLongZero(@SuppressWarnings("unused") PInt left, @SuppressWarnings("unused") long right) {
654+
return 0;
655+
}
656+
657+
@Specialization(guards = "right == 1")
658+
PInt doPIntLongOne(PInt left, @SuppressWarnings("unused") long right) {
659+
// we must return a new object with the same value
660+
return factory().createInt(left.getValue());
661+
}
662+
663+
@Specialization(guards = {"right != 0", "right != 1"})
653664
PInt doPIntLong(PInt left, long right) {
654665
return factory().createInt(mul(left.getValue(), BigInteger.valueOf(right)));
655666
}
@@ -661,7 +672,7 @@ PInt doPIntPInt(PInt left, PInt right) {
661672

662673
@TruffleBoundary
663674
BigInteger mul(BigInteger a, BigInteger b) {
664-
if (b.and(b.subtract(BigInteger.ONE)).equals(BigInteger.ZERO)) {
675+
if (!BigInteger.ZERO.equals(b) && b.and(b.subtract(BigInteger.ONE)).equals(BigInteger.ZERO)) {
665676
return bigIntegerShift(a, b.getLowestSetBit());
666677
} else {
667678
return bigIntegerMul(a, b);

0 commit comments

Comments
 (0)