From 305e1a749ff1b39e6c2009fb25111cca24f7a874 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 8 Dec 2012 11:05:50 +0100 Subject: Issue #16628: Fix a memory leak in ctypes.resize(). --- 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 index 3894d2e..3c9f382 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -175,6 +175,8 @@ Core and Builtins Library ------- +- Issue #16628: Fix a memory leak in ctypes.resize(). + - Issue #13120: Allow to call pdb.set_trace() from thread. Patch by Ilya Sandler. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 8be5958..603b3b8 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2442,7 +2442,7 @@ PyCData_clear(CDataObject *self) 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 index 1f3c1c0..3465995 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -1666,7 +1666,7 @@ resize(PyObject *self, PyObject *args) 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 index 27abe65..5237ac2 100644 --- a/Modules/_ctypes/ctypes.h +++ b/Modules/_ctypes/ctypes.h @@ -116,6 +116,7 @@ extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palig 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) -- cgit v0.12