Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,23 @@ def __next__(self):
l8 = self.iter_error(map(pack, Iter(3), "AB", strict=True), ValueError)
self.assertEqual(l8, [(2, "A"), (1, "B")])

# gh-140517: fix refcount leak
def test_map_strict_noleak(self):
t1 = (None, object())
t2 = (object(), object())
t3 = (object(),)

self.assertRaises(ValueError, tuple,
map(pack, t1, 'a', strict=True))
self.assertRaises(ValueError, tuple,
map(pack, t1, t2, 'a', strict=True))
self.assertRaises(ValueError, tuple,
map(pack, t1, t2, t3, strict=True))
self.assertRaises(ValueError, tuple,
map(pack, 'a', t1, strict=True))
self.assertRaises(ValueError, tuple,
map(pack, 'a', t2, t3, strict=True))

def test_max(self):
self.assertEqual(max('123123'), '3')
self.assertEqual(max(1, 2, 3), 3)
Expand Down
38 changes: 21 additions & 17 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1515,52 +1515,56 @@ map_next(PyObject *self)
}

result = _PyObject_VectorcallTstate(tstate, lz->func, stack, nargs, NULL);
goto exit;

exit:
for (i=0; i < nargs; i++) {
Py_DECREF(stack[i]);
}
if (stack != small_stack) {
PyMem_Free(stack);
}
return result;
check:
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
// next() on argument i raised an exception (not StopIteration)
return NULL;
// result is NULL;
goto exit;
}
PyErr_Clear();
}
if (i) {
// ValueError: map() argument 2 is shorter than argument 1
// ValueError: map() argument 3 is shorter than arguments 1-2
const char* plural = i == 1 ? " " : "s 1-";
return PyErr_Format(PyExc_ValueError,
"map() argument %d is shorter than argument%s%d",
i + 1, plural, i);
result = PyErr_Format(PyExc_ValueError,
"map() argument %d is shorter than argument%s%d",
i + 1, plural, i);
goto exit;
}
for (i = 1; i < niters; i++) {
PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
PyObject *val = (*Py_TYPE(it)->tp_iternext)(it);
if (val) {
Py_DECREF(val);
const char* plural = i == 1 ? " " : "s 1-";
return PyErr_Format(PyExc_ValueError,
"map() argument %d is longer than argument%s%d",
i + 1, plural, i);
result = PyErr_Format(PyExc_ValueError,
"map() argument %d is longer than argument%s%d",
i + 1, plural, i);
goto exit;
}
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
// next() on argument i raised an exception (not StopIteration)
return NULL;
// result is NULL;
goto exit;
}
PyErr_Clear();
}
// Argument i is exhausted. So far so good...
}
// All arguments are exhausted. Success!
goto exit;
exit:
for (i=0; i < nargs; i++) {
Py_DECREF(stack[i]);
}
if (stack != small_stack) {
PyMem_Free(stack);
}
return result;
}

static PyObject *
Expand Down
Loading