Commit eda70b81 authored by Daniel Stutzbach's avatar Daniel Stutzbach

#11335: Fix memory leak after key function failure in sort

parent 0d5e52d3
...@@ -111,6 +111,12 @@ class TestBase(unittest.TestCase): ...@@ -111,6 +111,12 @@ class TestBase(unittest.TestCase):
s.sort(key=CmpToKey(lambda a, b: int(random.random() * 3) - 1)) s.sort(key=CmpToKey(lambda a, b: int(random.random() * 3) - 1))
check("an insane function left some permutation", x, s) check("an insane function left some permutation", x, s)
if len(x) >= 2:
def bad_key(x):
raise RuntimeError
s = x[:]
self.assertRaises(RuntimeError, s.sort, key=bad_key)
x = [Complains(i) for i in x] x = [Complains(i) for i in x]
s = x[:] s = x[:]
random.shuffle(s) random.shuffle(s)
......
...@@ -1944,6 +1944,8 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds) ...@@ -1944,6 +1944,8 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds)
if (keys[i] == NULL) { if (keys[i] == NULL) {
for (i=i-1 ; i>=0 ; i--) for (i=i-1 ; i>=0 ; i--)
Py_DECREF(keys[i]); Py_DECREF(keys[i]);
if (keys != &ms.temparray[saved_ob_size+1])
PyMem_FREE(keys);
goto keyfunc_fail; goto keyfunc_fail;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment