Skip to content

Commit 2bea771

Browse files
tirkarthiserhiy-storchaka
authored andcommitted
bpo-34229: Check start and stop of slice object to be long when they are not int in PySlice_GetIndices (GH-8480)
1 parent 1c8f655 commit 2bea771

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

Lib/test/test_capi.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ def test(self):
108108
self.assertEqual(_testcapi.argparsing("Hello", "World"), 1)
109109

110110

111+
class TestGetIndices(unittest.TestCase):
112+
113+
def test_get_indices(self):
114+
self.assertEqual(_testcapi.get_indices(slice(10L, 20, 1), 100), (0, 10, 20, 1))
115+
self.assertEqual(_testcapi.get_indices(slice(10.1, 20, 1), 100), None)
116+
self.assertEqual(_testcapi.get_indices(slice(10, 20L, 1), 100), (0, 10, 20, 1))
117+
self.assertEqual(_testcapi.get_indices(slice(10, 20.1, 1), 100), None)
118+
119+
self.assertEqual(_testcapi.get_indices(slice(10L, 20, 1L), 100), (0, 10, 20, 1))
120+
self.assertEqual(_testcapi.get_indices(slice(10.1, 20, 1L), 100), None)
121+
self.assertEqual(_testcapi.get_indices(slice(10, 20L, 1L), 100), (0, 10, 20, 1))
122+
self.assertEqual(_testcapi.get_indices(slice(10, 20.1, 1L), 100), None)
123+
124+
111125
class SkipitemTest(unittest.TestCase):
112126

113127
def test_skipitem(self):
@@ -266,7 +280,7 @@ def test_main():
266280
raise support.TestFailed, sys.exc_info()[1]
267281

268282
support.run_unittest(CAPITest, TestPendingCalls, SkipitemTest,
269-
TestThreadState)
283+
TestThreadState, TestGetIndices)
270284

271285
if __name__ == "__main__":
272286
test_main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Check start and stop of slice object to be long when they are not int in
2+
:c:func:`PySlice_GetIndices`.

Modules/_testcapimodule.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,29 @@ getargs_et_hash(PyObject *self, PyObject *args)
15591559
return result;
15601560
}
15611561

1562+
static PyObject *
1563+
get_indices(PyObject *self, PyObject *args)
1564+
{
1565+
int result;
1566+
PySliceObject *slice;
1567+
Py_ssize_t length, start, stop, step;
1568+
1569+
if (!PyArg_ParseTuple(args, "On", &slice, &length))
1570+
return NULL;
1571+
1572+
result = PySlice_GetIndices(slice, length, &start, &stop, &step);
1573+
1574+
if (PyErr_Occurred()) {
1575+
assert(result == -1);
1576+
return NULL;
1577+
}
1578+
1579+
if (result == -1) {
1580+
Py_RETURN_NONE;
1581+
}
1582+
return Py_BuildValue("innn", result, start, stop, step);
1583+
}
1584+
15621585
static PyObject *
15631586
parse_tuple_and_keywords(PyObject *self, PyObject *args)
15641587
{
@@ -2664,6 +2687,7 @@ static PyMethodDef TestMethods[] = {
26642687
#ifdef Py_USING_UNICODE
26652688
{"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
26662689
#endif
2690+
{"get_indices", get_indices, METH_VARARGS},
26672691
{"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
26682692
{"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
26692693
{"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},

Objects/sliceobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
113113
if (r->start == Py_None) {
114114
*start = *step < 0 ? length-1 : 0;
115115
} else {
116-
if (!PyInt_Check(r->start) && !PyLong_Check(r->step)) return -1;
116+
if (!PyInt_Check(r->start) && !PyLong_Check(r->start)) return -1;
117117
*start = PyInt_AsSsize_t(r->start);
118118
if (*start < 0) *start += length;
119119
}
120120
if (r->stop == Py_None) {
121121
*stop = *step < 0 ? -1 : length;
122122
} else {
123-
if (!PyInt_Check(r->stop) && !PyLong_Check(r->step)) return -1;
123+
if (!PyInt_Check(r->stop) && !PyLong_Check(r->stop)) return -1;
124124
*stop = PyInt_AsSsize_t(r->stop);
125125
if (*stop < 0) *stop += length;
126126
}

0 commit comments

Comments
 (0)