[Python-checkins] cpython (2.7): Issue #16628: Fix a memory leak in ctypes.resize().

antoine.pitrou python-checkins at python.org
Sat Dec 8 11:10:25 CET 2012


http://hg.python.org/cpython/rev/df5a86a22310
changeset:   80746:df5a86a22310
branch:      2.7
parent:      80737:0fd2ea06e17d
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Dec 08 11:05:50 2012 +0100
summary:
  Issue #16628: Fix a memory leak in ctypes.resize().

files:
  Misc/NEWS                  |  2 ++
  Modules/_ctypes/_ctypes.c  |  2 +-
  Modules/_ctypes/callproc.c |  2 +-
  Modules/_ctypes/ctypes.h   |  1 +
  4 files changed, 5 insertions(+), 2 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -156,6 +156,8 @@
 Library
 -------
 
+- Issue #16628: Fix a memory leak in ctypes.resize().
+
 - Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
   Patch by Serhiy Storchaka.
 
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -2530,7 +2530,7 @@
     assert(dict); /* Cannot be NULL for CDataObject instances */
     Py_CLEAR(self->b_objects);
     if ((self->b_needsfree)
-        && ((size_t)dict->size > sizeof(self->b_value)))
+        && _CDataObject_HasExternalBuffer(self))
         PyMem_Free(self->b_ptr);
     self->b_ptr = NULL;
     Py_CLEAR(self->b_base);
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -1740,7 +1740,7 @@
         obj->b_size = size;
         goto done;
     }
-    if (obj->b_size <= sizeof(obj->b_value)) {
+    if (!_CDataObject_HasExternalBuffer(obj)) {
         /* We are currently using the objects default buffer, but it
            isn't large enough any more. */
         void *ptr = PyMem_Malloc(size);
diff --git a/Modules/_ctypes/ctypes.h b/Modules/_ctypes/ctypes.h
--- a/Modules/_ctypes/ctypes.h
+++ b/Modules/_ctypes/ctypes.h
@@ -153,6 +153,7 @@
 extern PyTypeObject PyCData_Type;
 #define CDataObject_CheckExact(v)       ((v)->ob_type == &PyCData_Type)
 #define CDataObject_Check(v)            PyObject_TypeCheck(v, &PyCData_Type)
+#define _CDataObject_HasExternalBuffer(v)  ((v)->b_ptr != (char *)&(v)->b_value)
 
 extern PyTypeObject PyCSimpleType_Type;
 #define PyCSimpleTypeObject_CheckExact(v)       ((v)->ob_type == &PyCSimpleType_Type)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list