Issue #16628: Fix a memory leak in ctypes.resize().

This commit is contained in:
Antoine Pitrou 2012-12-08 11:05:50 +01:00
parent 766849b978
commit 8eeda72742
4 changed files with 5 additions and 2 deletions

View File

@ -156,6 +156,8 @@ Core and Builtins
Library Library
------- -------
- Issue #16628: Fix a memory leak in ctypes.resize().
- Issue #10182: The re module doesn't truncate indices to 32 bits anymore. - Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
Patch by Serhiy Storchaka. Patch by Serhiy Storchaka.

View File

@ -2530,7 +2530,7 @@ PyCData_clear(CDataObject *self)
assert(dict); /* Cannot be NULL for CDataObject instances */ assert(dict); /* Cannot be NULL for CDataObject instances */
Py_CLEAR(self->b_objects); Py_CLEAR(self->b_objects);
if ((self->b_needsfree) if ((self->b_needsfree)
&& ((size_t)dict->size > sizeof(self->b_value))) && _CDataObject_HasExternalBuffer(self))
PyMem_Free(self->b_ptr); PyMem_Free(self->b_ptr);
self->b_ptr = NULL; self->b_ptr = NULL;
Py_CLEAR(self->b_base); Py_CLEAR(self->b_base);

View File

@ -1740,7 +1740,7 @@ resize(PyObject *self, PyObject *args)
obj->b_size = size; obj->b_size = size;
goto done; goto done;
} }
if (obj->b_size <= sizeof(obj->b_value)) { if (!_CDataObject_HasExternalBuffer(obj)) {
/* We are currently using the objects default buffer, but it /* We are currently using the objects default buffer, but it
isn't large enough any more. */ isn't large enough any more. */
void *ptr = PyMem_Malloc(size); void *ptr = PyMem_Malloc(size);

View File

@ -153,6 +153,7 @@ extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palig
extern PyTypeObject PyCData_Type; extern PyTypeObject PyCData_Type;
#define CDataObject_CheckExact(v) ((v)->ob_type == &PyCData_Type) #define CDataObject_CheckExact(v) ((v)->ob_type == &PyCData_Type)
#define CDataObject_Check(v) PyObject_TypeCheck(v, &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; extern PyTypeObject PyCSimpleType_Type;
#define PyCSimpleTypeObject_CheckExact(v) ((v)->ob_type == &PyCSimpleType_Type) #define PyCSimpleTypeObject_CheckExact(v) ((v)->ob_type == &PyCSimpleType_Type)