mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
SF bug 555042: zip() may trigger MemoryError.
NOT a bugfix candidate: this is a fix to an optimization introduced in 2.3.
This commit is contained in:
parent
9b1df1db68
commit
39a86c2188
2 changed files with 18 additions and 3 deletions
|
@ -344,7 +344,17 @@ except:
|
||||||
if not exc:
|
if not exc:
|
||||||
raise TestFailed, 'zip(a, b) - missing expected TypeError'
|
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
|
# Epilogue -- unlink the temp file
|
||||||
|
|
||||||
unlink(TESTFN)
|
unlink(TESTFN)
|
||||||
|
|
|
@ -1717,13 +1717,18 @@ builtin_zip(PyObject *self, PyObject *args)
|
||||||
/* args must be a tuple */
|
/* args must be a tuple */
|
||||||
assert(PyTuple_Check(args));
|
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 */
|
len = -1; /* unknown */
|
||||||
for (i = 0; i < itemsize; ++i) {
|
for (i = 0; i < itemsize; ++i) {
|
||||||
PyObject *item = PyTuple_GET_ITEM(args, i);
|
PyObject *item = PyTuple_GET_ITEM(args, i);
|
||||||
int thislen = PySequence_Length(item);
|
int thislen = PySequence_Length(item);
|
||||||
if (thislen < 0)
|
if (thislen < 0) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
len = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
else if (len < 0 || thislen < len)
|
else if (len < 0 || thislen < len)
|
||||||
len = thislen;
|
len = thislen;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue