summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2015-02-02 02:35:34 (GMT)
committerBenjamin Peterson <benjamin@python.org>2015-02-02 02:35:34 (GMT)
commitc468b537cd227ce5484500a8f8ea7e932eabc7d5 (patch)
tree6f69d38e1640f2b243ae148e62424d605a6ee4d8
parentf635dc32b1dc3d322acd281921afdb8c13261838 (diff)
parent0eaabf1c05127793753dbb3641d4d107b284ae77 (diff)
downloadcpython-c468b537cd227ce5484500a8f8ea7e932eabc7d5.zip
cpython-c468b537cd227ce5484500a8f8ea7e932eabc7d5.tar.gz
cpython-c468b537cd227ce5484500a8f8ea7e932eabc7d5.tar.bz2
merge 3.3 (#23364, #23363)
-rw-r--r--Lib/test/test_itertools.py12
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/itertoolsmodule.c18
3 files changed, 32 insertions, 2 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 1a5c79c..207ee9e 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -426,6 +426,13 @@ class TestBasicOps(unittest.TestCase):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.pickletest(proto, permutations(values, r)) # test pickling
+ @support.bigaddrspacetest
+ def test_permutations_overflow(self):
+ with self.assertRaises(OverflowError):
+ 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):
self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1)
@@ -955,6 +962,11 @@ class TestBasicOps(unittest.TestCase):
args = map(iter, args)
self.assertEqual(len(list(product(*args))), expected_len)
+ @support.bigaddrspacetest
+ def test_product_overflow(self):
+ with self.assertRaises(OverflowError):
+ product(["a"]*(2**16), repeat=2**16)
+
@support.impl_detail("tuple reuse is specific to CPython")
def test_product_tuple_reuse(self):
self.assertEqual(len(set(map(id, product('abc', 'def')))), 1)
diff --git a/Misc/NEWS b/Misc/NEWS
index 0972950..d0ee519 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -50,6 +50,10 @@ Core and Builtins
Library
-------
+- Issue #23363: Fix possible overflow in itertools.permutations.
+
+- Issue #23364: Fix possible overflow in itertools.product.
+
- Issue #23366: Fixed possible integer overflow in itertools.combinations.
- Issue #23366: Fixed possible integer overflow in itertools.combinations.
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 610799f..1e0d49a 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -2017,8 +2017,17 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
}
- assert(PyTuple_Check(args));
- nargs = (repeat == 0) ? 0 : PyTuple_GET_SIZE(args);
+ assert(PyTuple_CheckExact(args));
+ if (repeat == 0) {
+ 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))) {
+ PyErr_SetString(PyExc_OverflowError, "repeat argument too large");
+ return NULL;
+ }
+ }
npools = nargs * repeat;
indices = PyMem_Malloc(npools * sizeof(Py_ssize_t));
@@ -3049,6 +3058,11 @@ 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));
if (indices == NULL || cycles == NULL) {