Skip to content

Commit ed65249

Browse files
committed
Raise OverflowError when converting too large integers to float
1 parent cec4776 commit ed65249

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626
package com.oracle.graal.python.builtins.modules;
2727

28+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.OverflowError;
2829
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_ADD_NATIVE_SLOTS;
2930
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_OBJECT_GENERIC_NEW;
3031
import static com.oracle.graal.python.builtins.objects.slice.PSlice.MISSING_INDEX;
@@ -967,11 +968,16 @@ Object floatFromLong(LazyPythonClass cls, long arg) {
967968
}
968969

969970
@Specialization(guards = "!isNativeClass(cls)")
970-
Object floatFromPInt(LazyPythonClass cls, PInt arg) {
971+
Object floatFromPInt(LazyPythonClass cls, PInt arg,
972+
@Cached PRaiseNode raise) {
973+
double value = arg.doubleValue();
974+
if (Double.isInfinite(value)) {
975+
throw raise.raise(OverflowError, "int too large to convert to float");
976+
}
971977
if (isPrimitiveFloat(cls)) {
972-
return arg.doubleValue();
978+
return value;
973979
}
974-
return factory().createFloat(cls, arg.doubleValue());
980+
return factory().createFloat(cls, value);
975981
}
976982

977983
@Specialization(guards = "!isNativeClass(cls)")
@@ -2544,7 +2550,7 @@ private String mangle(String privateobj, String ident) {
25442550
plen -= ipriv;
25452551

25462552
if ((long) plen + nlen >= Integer.MAX_VALUE) {
2547-
throw raise(PythonBuiltinClassType.OverflowError, "private identifier too large to be mangled");
2553+
throw raise(OverflowError, "private identifier too large to be mangled");
25482554
}
25492555

25502556
/* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToDoubleNode.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.nodes.util;
4242

43+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
4344
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
4445

4546
import com.oracle.graal.python.builtins.modules.MathGuards;
@@ -75,8 +76,13 @@ public double toDouble(long x) {
7576
}
7677

7778
@Specialization
78-
public double toDouble(PInt x) {
79-
return x.doubleValue();
79+
public double toDouble(PInt x,
80+
@Cached PRaiseNode raise) {
81+
double value = x.doubleValue();
82+
if (Double.isInfinite(value)) {
83+
throw raise.raise(OverflowError, "int too large to convert to float");
84+
}
85+
return value;
8086
}
8187

8288
@Specialization

0 commit comments

Comments
 (0)