]> granicus.if.org Git - python/commitdiff
Issues #23363, #23364, #23365, #23366: Fixed itertools overflow tests.
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 2 Feb 2015 23:34:09 +0000 (01:34 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 2 Feb 2015 23:34:09 +0000 (01:34 +0200)
Used PyMem_New to check overflow.

Lib/test/test_itertools.py
Modules/itertoolsmodule.c

index 3c7dd499952b88cc9206cc1396d4cbbdc6379f94..bde7ab6ddea599825025950a293aa270e64cac5f 100644 (file)
@@ -139,7 +139,7 @@ class TestBasicOps(unittest.TestCase):
 
     @test_support.bigaddrspacetest
     def test_combinations_overflow(self):
-        with self.assertRaises(OverflowError):
+        with self.assertRaises((OverflowError, MemoryError)):
             combinations("AA", 2**29)
 
     @test_support.impl_detail("tuple reuse is specific to CPython")
@@ -215,7 +215,7 @@ class TestBasicOps(unittest.TestCase):
 
     @test_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_support.impl_detail("tuple reuse is specific to CPython")
@@ -286,10 +286,8 @@ class TestBasicOps(unittest.TestCase):
 
     @test_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)
 
     @test_support.impl_detail("tuple reuse is specific to CPython")
     def test_permutations_tuple_reuse(self):
@@ -711,8 +709,8 @@ class TestBasicOps(unittest.TestCase):
 
     @test_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)
 
     @test_support.impl_detail("tuple reuse is specific to CPython")
     def test_product_tuple_reuse(self):
index b63975c1e749fb2257f409a73b2077865a5cc7b8..e5b2b7443954b20f46e234b007c57a07270c9c03 100644 (file)
@@ -1847,15 +1847,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;
@@ -2102,11 +2101,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;
@@ -2355,11 +2350,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;
@@ -2612,13 +2603,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;