Skip to content

Commit a7be968

Browse files
abdelbernielkorchi
authored andcommitted
[GR-69541] Backport to 25.0: Fix integer division with large negative divisors
PullRequest: graalpython/4001
2 parents b37047d + 1b2aef2 commit a7be968

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,4 @@ pom-mx.xml
9797
!graalpython/com.oracle.graal.python.test/src/tests/standalone/gradle/gradle-test-project/gradle/wrapper/gradle-wrapper.jar
9898
/*-venv/
9999
.aider*
100+
*.iprof.gz

graalpython/com.oracle.graal.python.test/src/tests/test_int.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ def test_bigint_mul():
150150
assert 99999937497465632974931 * 1223432423545234234123123 == 122343165886896325043539375228725106116626429513
151151
assert 99999937497465632974931 * (2**100) == 126764980791447734004805377032945185921379990352429056
152152

153+
def test_floordiv():
154+
assert 0 // (-92233720368547394651) == 0
155+
assert -234 // (-92233720368547394651) == 0
156+
assert 234 // (-92233720368547394651) == -1
153157

154158
def test_pow():
155159
assert 2 ** 10 == 1024

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
package com.oracle.graal.python.builtins.objects.ints;
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.DeprecationWarning;
44-
import static com.oracle.graal.python.nodes.ErrorMessages.BITWISE_INVERSION_OF_THE_UNDERLYING_INT;
4544
import static com.oracle.graal.python.nodes.BuiltinNames.J_INT;
45+
import static com.oracle.graal.python.nodes.ErrorMessages.BITWISE_INVERSION_OF_THE_UNDERLYING_INT;
4646
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___CEIL__;
4747
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___FLOOR__;
4848
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___FORMAT__;
@@ -813,7 +813,7 @@ static Object doIPi(int left, PInt right,
813813
}
814814
return Math.floorDiv(left, rightValue);
815815
} catch (OverflowException e) {
816-
return left < 0 == right.isNegative() ? 0 : -1;
816+
return left == 0 || left < 0 == right.isNegative() ? 0 : -1;
817817
}
818818
}
819819

@@ -831,7 +831,7 @@ static Object doLPi(long left, PInt right,
831831
}
832832
return Math.floorDiv(left, rightValue);
833833
} catch (OverflowException e) {
834-
return left < 0 == right.isNegative() ? 0 : -1;
834+
return left == 0 || left < 0 == right.isNegative() ? 0 : -1;
835835
}
836836
}
837837

0 commit comments

Comments
 (0)