changeset 68210:4dac4769ea24

#11335: Fix memory leak when a sort key function throws an exception
author Daniel Stutzbach <daniel@stutzbachenterprises.com>
date Wed, 02 Mar 2011 23:37:50 +0000
parents a1dbe53f3798
children 6b12d2b68f25
files Misc/NEWS Objects/listobject.c
diffstat 2 files changed, 5 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Misc/NEWS	Wed Mar 02 01:21:46 2011 +0000
+++ b/Misc/NEWS	Wed Mar 02 23:37:50 2011 +0000
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #11335: Fixed a memory leak in list.sort when the key function
+  throws an exception.
+
 - Issue #8923: When a string is encoded to UTF-8 in strict mode, the result is
   cached into the object. Examples: str.encode(), str.encode('utf-8'),
   PyUnicode_AsUTF8String() and PyUnicode_AsEncodedString(unicode, "utf-8",
--- a/Objects/listobject.c	Wed Mar 02 01:21:46 2011 +0000
+++ b/Objects/listobject.c	Wed Mar 02 23:37:50 2011 +0000
@@ -1957,6 +1957,8 @@
             if (keys[i] == NULL) {
                 for (i=i-1 ; i>=0 ; i--)
                     Py_DECREF(keys[i]);
+                if (keys != &ms.temparray[saved_ob_size+1])
+                    PyMem_FREE(keys);
                 goto keyfunc_fail;
             }
         }