Skip to content

Commit dee948b

Browse files
Issues python#23363, python#23364, python#23365, python#23366: Fixed itertools overflow tests.
Used PyMem_New to check overflow.
1 parent 1572944 commit dee948b

2 files changed

Lines changed: 11 additions & 27 deletions

File tree

Lib/test/test_itertools.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def combinations3(iterable, r):
260260

261261
@support.bigaddrspacetest
262262
def test_combinations_overflow(self):
263-
with self.assertRaises(OverflowError):
263+
with self.assertRaises((OverflowError, MemoryError)):
264264
combinations("AA", 2**29)
265265

266266
# Test implementation detail: tuple re-use
@@ -346,7 +346,7 @@ def numcombs(n, r):
346346

347347
@support.bigaddrspacetest
348348
def test_combinations_with_replacement_overflow(self):
349-
with self.assertRaises(OverflowError):
349+
with self.assertRaises((OverflowError, MemoryError)):
350350
combinations_with_replacement("AA", 2**30)
351351

352352
# Test implementation detail: tuple re-use
@@ -420,10 +420,8 @@ def permutations2(iterable, r=None):
420420

421421
@support.bigaddrspacetest
422422
def test_permutations_overflow(self):
423-
with self.assertRaises(OverflowError):
423+
with self.assertRaises((OverflowError, MemoryError)):
424424
permutations("A", 2**30)
425-
with self.assertRaises(OverflowError):
426-
permutations("A", 2, 2**30)
427425

428426
@support.impl_detail("tuple resuse is CPython specific")
429427
def test_permutations_tuple_reuse(self):
@@ -939,8 +937,8 @@ def product2(*args, **kwds):
939937

940938
@support.bigaddrspacetest
941939
def test_product_overflow(self):
942-
with self.assertRaises(OverflowError):
943-
product(["a"]*(2**16), repeat=2**16)
940+
with self.assertRaises((OverflowError, MemoryError)):
941+
product(*(['ab']*2**5), repeat=2**25)
944942

945943
@support.impl_detail("tuple reuse is specific to CPython")
946944
def test_product_tuple_reuse(self):

Modules/itertoolsmodule.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,15 +2003,14 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20032003
nargs = 0;
20042004
} else {
20052005
nargs = PyTuple_GET_SIZE(args);
2006-
if (repeat > PY_SSIZE_T_MAX/sizeof(Py_ssize_t) ||
2007-
nargs > PY_SSIZE_T_MAX/(repeat * sizeof(Py_ssize_t))) {
2006+
if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) {
20082007
PyErr_SetString(PyExc_OverflowError, "repeat argument too large");
20092008
return NULL;
20102009
}
20112010
}
20122011
npools = nargs * repeat;
20132012

2014-
indices = PyMem_Malloc(npools * sizeof(Py_ssize_t));
2013+
indices = PyMem_New(Py_ssize_t, npools);
20152014
if (indices == NULL) {
20162015
PyErr_NoMemory();
20172016
goto error;
@@ -2335,11 +2334,7 @@ combinations_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23352334
goto error;
23362335
}
23372336

2338-
if (r > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)) {
2339-
PyErr_SetString(PyExc_OverflowError, "r is too big");
2340-
goto error;
2341-
}
2342-
indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
2337+
indices = PyMem_New(Py_ssize_t, r);
23432338
if (indices == NULL) {
23442339
PyErr_NoMemory();
23452340
goto error;
@@ -2668,11 +2663,7 @@ cwr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26682663
goto error;
26692664
}
26702665

2671-
if (r > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)) {
2672-
PyErr_SetString(PyExc_OverflowError, "r is too big");
2673-
goto error;
2674-
}
2675-
indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
2666+
indices = PyMem_New(Py_ssize_t, r);
26762667
if (indices == NULL) {
26772668
PyErr_NoMemory();
26782669
goto error;
@@ -3001,13 +2992,8 @@ permutations_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
30012992
goto error;
30022993
}
30032994

3004-
if (n > PY_SSIZE_T_MAX/sizeof(Py_ssize_t) ||
3005-
r > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)) {
3006-
PyErr_SetString(PyExc_OverflowError, "parameters too large");
3007-
goto error;
3008-
}
3009-
indices = PyMem_Malloc(n * sizeof(Py_ssize_t));
3010-
cycles = PyMem_Malloc(r * sizeof(Py_ssize_t));
2995+
indices = PyMem_New(Py_ssize_t, n);
2996+
cycles = PyMem_New(Py_ssize_t, r);
30112997
if (indices == NULL || cycles == NULL) {
30122998
PyErr_NoMemory();
30132999
goto error;

0 commit comments

Comments
 (0)