summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-02-02 23:50:31 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-02-02 23:50:31 (GMT)
commit7a27c97216ef1a71bc26a559d7331523875a0c6f (patch)
tree9eddc670b521a45b275d3944812f16ea501b2540
parent83848704f550912fe559988d6de0b5105844d75f (diff)
parentb5e8e5755586820a278326e2aa55b8ff755f0a59 (diff)
downloadcpython-7a27c97216ef1a71bc26a559d7331523875a0c6f.zip
cpython-7a27c97216ef1a71bc26a559d7331523875a0c6f.tar.gz
cpython-7a27c97216ef1a71bc26a559d7331523875a0c6f.tar.bz2
Issues #23363, #23364, #23365, #23366: Fixed itertools overflow tests.
Used PyMem_New to check overflow.
-rw-r--r--Lib/test/test_itertools.py12
-rw-r--r--Modules/itertoolsmodule.c26
2 files changed, 11 insertions, 27 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index d0167e5..6a0130e 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -266,7 +266,7 @@ class TestBasicOps(unittest.TestCase):
@support.bigaddrspacetest
def test_combinations_overflow(self):
- with self.assertRaises(OverflowError):
+ with self.assertRaises((OverflowError, MemoryError)):
combinations("AA", 2**29)
# Test implementation detail: tuple re-use
@@ -353,7 +353,7 @@ class TestBasicOps(unittest.TestCase):
@support.bigaddrspacetest
def test_combinations_with_replacement_overflow(self):
- with self.assertRaises(OverflowError):
+ with self.assertRaises((OverflowError, MemoryError)):
combinations_with_replacement("AA", 2**30)
# Test implementation detail: tuple re-use
@@ -428,10 +428,8 @@ class TestBasicOps(unittest.TestCase):
@support.bigaddrspacetest
def test_permutations_overflow(self):
- with self.assertRaises(OverflowError):
+ with self.assertRaises((OverflowError, MemoryError)):
permutations("A", 2**30)
- with self.assertRaises(OverflowError):
- permutations("A", 2, 2**30)
@support.impl_detail("tuple reuse is specific to CPython")
def test_permutations_tuple_reuse(self):
@@ -964,8 +962,8 @@ class TestBasicOps(unittest.TestCase):
@support.bigaddrspacetest
def test_product_overflow(self):
- with self.assertRaises(OverflowError):
- product(["a"]*(2**16), repeat=2**16)
+ with self.assertRaises((OverflowError, MemoryError)):
+ product(*(['ab']*2**5), repeat=2**25)
@support.impl_detail("tuple reuse is specific to CPython")
def test_product_tuple_reuse(self):
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 1e0d49a..1f42022 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -2022,15 +2022,14 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
nargs = 0;
} else {
nargs = PyTuple_GET_SIZE(args);
- if (repeat > PY_SSIZE_T_MAX/sizeof(Py_ssize_t) ||
- nargs > PY_SSIZE_T_MAX/(repeat * sizeof(Py_ssize_t))) {
+ if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) {
PyErr_SetString(PyExc_OverflowError, "repeat argument too large");
return NULL;
}
}
npools = nargs * repeat;
- indices = PyMem_Malloc(npools * sizeof(Py_ssize_t));
+ indices = PyMem_New(Py_ssize_t, npools);
if (indices == NULL) {
PyErr_NoMemory();
goto error;
@@ -2368,11 +2367,7 @@ combinations_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
goto error;
}
- if (r > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)) {
- PyErr_SetString(PyExc_OverflowError, "r is too big");
- goto error;
- }
- indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
+ indices = PyMem_New(Py_ssize_t, r);
if (indices == NULL) {
PyErr_NoMemory();
goto error;
@@ -2713,11 +2708,7 @@ cwr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
goto error;
}
- if (r > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)) {
- PyErr_SetString(PyExc_OverflowError, "r is too big");
- goto error;
- }
- indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
+ indices = PyMem_New(Py_ssize_t, r);
if (indices == NULL) {
PyErr_NoMemory();
goto error;
@@ -3058,13 +3049,8 @@ permutations_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
goto error;
}
- if (n > PY_SSIZE_T_MAX/sizeof(Py_ssize_t) ||
- r > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)) {
- PyErr_SetString(PyExc_OverflowError, "parameters too large");
- goto error;
- }
- indices = PyMem_Malloc(n * sizeof(Py_ssize_t));
- cycles = PyMem_Malloc(r * sizeof(Py_ssize_t));
+ indices = PyMem_New(Py_ssize_t, n);
+ cycles = PyMem_New(Py_ssize_t, r);
if (indices == NULL || cycles == NULL) {
PyErr_NoMemory();
goto error;