Skip to content

Commit ac69a10

Browse files
committed
Fix: Do correct overflow handling in 'PySlice_Unpack'.
1 parent 9829a54 commit ac69a10

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,27 +1264,28 @@ Object get() {
12641264
}
12651265

12661266
@Builtin(name = "PyTruffleSlice_GetIndicesEx", fixedNumOfArguments = 4)
1267+
@TypeSystemReference(PythonArithmeticTypes.class)
12671268
@GenerateNodeFactory
12681269
abstract static class PyTruffleSlice_GetIndicesEx extends NativeBuiltin {
12691270
@Specialization
1270-
Object doUnpack(int start, int stop, int step, long length) {
1271+
Object doUnpack(int start, int stop, int step, int length) {
12711272
PSlice tmpSlice = factory().createSlice(start, stop, step);
1272-
SliceInfo actualIndices = tmpSlice.computeActualIndices((int) length);
1273+
SliceInfo actualIndices = tmpSlice.computeActualIndices(length);
12731274
return factory().createTuple(new Object[]{actualIndices.start, actualIndices.stop, actualIndices.step, actualIndices.length});
12741275
}
12751276

12761277
@Specialization(rewriteOn = ArithmeticException.class)
12771278
Object doUnpackLong(long start, long stop, long step, long length) {
12781279
PSlice tmpSlice = factory().createSlice(PInt.intValueExact(start), PInt.intValueExact(stop), PInt.intValueExact(step));
1279-
SliceInfo actualIndices = tmpSlice.computeActualIndices((int) length);
1280+
SliceInfo actualIndices = tmpSlice.computeActualIndices(PInt.intValueExact(length));
12801281
return factory().createTuple(new Object[]{actualIndices.start, actualIndices.stop, actualIndices.step, actualIndices.length});
12811282
}
12821283

1283-
@Specialization(replaces = "doUnpackLong")
1284+
@Specialization(replaces = {"doUnpackLong", "doUnpack"})
12841285
Object doUnpackLongOvf(long start, long stop, long step, long length) {
12851286
try {
12861287
PSlice tmpSlice = factory().createSlice(PInt.intValueExact(start), PInt.intValueExact(stop), PInt.intValueExact(step));
1287-
SliceInfo actualIndices = tmpSlice.computeActualIndices((int) length);
1288+
SliceInfo actualIndices = tmpSlice.computeActualIndices(length > Integer.MAX_VALUE ? Integer.MAX_VALUE : PInt.intValueExact(length));
12881289
return factory().createTuple(new Object[]{actualIndices.start, actualIndices.stop, actualIndices.step, actualIndices.length});
12891290
} catch (ArithmeticException e) {
12901291
throw raiseIndexError();

graalpython/lib-graalpython/python_cext.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,9 +1233,10 @@ def PyRun_String(source, typ, globals, locals):
12331233
return exec(compile(source, typ, typ), globals, locals)
12341234

12351235

1236-
@may_raise
1236+
# called without landing; do conversion manually
1237+
@may_raise(to_sulong(error_handler))
12371238
def PySlice_GetIndicesEx(start, stop, step, length):
1238-
return PyTruffleSlice_GetIndicesEx(start, stop, step, length)
1239+
return to_sulong(PyTruffleSlice_GetIndicesEx(start, stop, step, length))
12391240

12401241

12411242
@may_raise

0 commit comments

Comments
 (0)