From 39a86c2188b22818c187e860fbf7d77a28a6a116 Mon Sep 17 00:00:00 2001 From: Tim Peters Date: Sun, 12 May 2002 07:19:38 +0000 Subject: [PATCH] SF bug 555042: zip() may trigger MemoryError. NOT a bugfix candidate: this is a fix to an optimization introduced in 2.3. --- Lib/test/test_b2.py | 12 +++++++++++- Python/bltinmodule.c | 9 +++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_b2.py b/Lib/test/test_b2.py index 72810cbf2b6..459fd6b851a 100644 --- a/Lib/test/test_b2.py +++ b/Lib/test/test_b2.py @@ -344,7 +344,17 @@ except: if not exc: raise TestFailed, 'zip(a, b) - missing expected TypeError' +# Make sure zip doesn't try to allocate a billion elements for the +# result list when one of its arguments doesn't say how long it is. +# A MemoryError is the most likely failure mode. +class SequenceWithoutALength: + def __getitem__(self, i): + if i == 5: + raise IndexError + else: + return i +vereq(zip(SequenceWithoutALength(), xrange(2**30)), + list(enumerate(range(5)))) # Epilogue -- unlink the temp file - unlink(TESTFN) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index fb6810edb8d..d0411e23ae7 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1717,13 +1717,18 @@ builtin_zip(PyObject *self, PyObject *args) /* args must be a tuple */ assert(PyTuple_Check(args)); - /* Guess at result length: the shortest of the input lengths. */ + /* Guess at result length: the shortest of the input lengths. + If some argument refuses to say, we refuse to guess too, lest + an argument like xrange(sys.maxint) lead us astray.*/ len = -1; /* unknown */ for (i = 0; i < itemsize; ++i) { PyObject *item = PyTuple_GET_ITEM(args, i); int thislen = PySequence_Length(item); - if (thislen < 0) + if (thislen < 0) { PyErr_Clear(); + len = -1; + break; + } else if (len < 0 || thislen < len) len = thislen; }