Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7410861

Browse files
Issue #27330: Fixed possible leaks in the ctypes module.
2 parents 0475ffa + adef646 commit 7410861

4 files changed

Lines changed: 33 additions & 22 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.6.0 alpha 3
1010
Library
1111
-------
1212

13+
- Issue #27330: Fixed possible leaks in the ctypes module.
14+
1315
- Issue #27238: Got rid of bare excepts in the turtle module. Original patch
1416
by Jelle Zijlstra.
1517

Modules/_ctypes/_ctypes.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,8 +1252,10 @@ add_methods(PyTypeObject *type, PyMethodDef *meth)
12521252
descr = PyDescr_NewMethod(type, meth);
12531253
if (descr == NULL)
12541254
return -1;
1255-
if (PyDict_SetItemString(dict,meth->ml_name, descr) < 0)
1255+
if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) {
1256+
Py_DECREF(descr);
12561257
return -1;
1258+
}
12571259
Py_DECREF(descr);
12581260
}
12591261
return 0;
@@ -1268,8 +1270,10 @@ add_members(PyTypeObject *type, PyMemberDef *memb)
12681270
descr = PyDescr_NewMember(type, memb);
12691271
if (descr == NULL)
12701272
return -1;
1271-
if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1273+
if (PyDict_SetItemString(dict, memb->name, descr) < 0) {
1274+
Py_DECREF(descr);
12721275
return -1;
1276+
}
12731277
Py_DECREF(descr);
12741278
}
12751279
return 0;
@@ -1285,8 +1289,10 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp)
12851289
descr = PyDescr_NewGetSet(type, gsp);
12861290
if (descr == NULL)
12871291
return -1;
1288-
if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1292+
if (PyDict_SetItemString(dict, gsp->name, descr) < 0) {
1293+
Py_DECREF(descr);
12891294
return -1;
1295+
}
12901296
Py_DECREF(descr);
12911297
}
12921298
return 0;
@@ -1778,6 +1784,7 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
17781784

17791785
newname = PyUnicode_Concat(name, suffix);
17801786
if (newname == NULL) {
1787+
Py_DECREF(swapped_args);
17811788
return NULL;
17821789
}
17831790

@@ -1797,8 +1804,10 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
17971804

17981805
stgdict = (StgDictObject *)PyObject_CallObject(
17991806
(PyObject *)&PyCStgDict_Type, NULL);
1800-
if (!stgdict) /* XXX leaks result! */
1807+
if (!stgdict) {
1808+
Py_DECREF(result);
18011809
return NULL;
1810+
}
18021811

18031812
stgdict->ffi_type_pointer = *fmt->pffi_type;
18041813
stgdict->align = fmt->pffi_type->alignment;
@@ -1978,8 +1987,10 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
19781987
PyObject *meth;
19791988
int x;
19801989
meth = PyDescr_NewClassMethod(result, ml);
1981-
if (!meth)
1990+
if (!meth) {
1991+
Py_DECREF(result);
19821992
return NULL;
1993+
}
19831994
x = PyDict_SetItemString(result->tp_dict,
19841995
ml->ml_name,
19851996
meth);
@@ -2159,8 +2170,10 @@ converters_from_argtypes(PyObject *ob)
21592170

21602171
nArgs = PyTuple_GET_SIZE(ob);
21612172
converters = PyTuple_New(nArgs);
2162-
if (!converters)
2173+
if (!converters) {
2174+
Py_DECREF(ob);
21632175
return NULL;
2176+
}
21642177

21652178
/* I have to check if this is correct. Using c_char, which has a size
21662179
of 1, will be assumed to be pushed as only one byte!

Modules/_ctypes/callproc.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ _ctypes_get_errobj(int **pspace)
157157
return NULL;
158158
memset(space, 0, sizeof(int) * 2);
159159
errobj = PyCapsule_New(space, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
160-
if (errobj == NULL)
160+
if (errobj == NULL) {
161+
PyMem_Free(space);
161162
return NULL;
163+
}
162164
if (-1 == PyDict_SetItem(dict, error_object_name,
163165
errobj)) {
164166
Py_DECREF(errobj);
@@ -1681,6 +1683,10 @@ POINTER(PyObject *self, PyObject *cls)
16811683
if (result == NULL)
16821684
return result;
16831685
key = PyLong_FromVoidPtr(result);
1686+
if (key == NULL) {
1687+
Py_DECREF(result);
1688+
return NULL;
1689+
}
16841690
} else if (PyType_Check(cls)) {
16851691
typ = (PyTypeObject *)cls;
16861692
buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1);

Modules/_ctypes/cfield.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,8 +1246,7 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
12461246
"unicode string expected instead of %s instance",
12471247
value->ob_type->tp_name);
12481248
return NULL;
1249-
} else
1250-
Py_INCREF(value);
1249+
}
12511250

12521251
wstr = PyUnicode_AsUnicodeAndSize(value, &size);
12531252
if (wstr == NULL)
@@ -1256,7 +1255,6 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
12561255
PyErr_Format(PyExc_ValueError,
12571256
"string too long (%zd, maximum length %zd)",
12581257
size, length);
1259-
Py_DECREF(value);
12601258
return NULL;
12611259
} else if (size < length-1)
12621260
/* copy terminating NUL character if there is space */
@@ -1266,6 +1264,7 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
12661264
return NULL;
12671265
}
12681266

1267+
Py_INCREF(value);
12691268
return value;
12701269
}
12711270

@@ -1292,35 +1291,29 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
12921291
char *data;
12931292
Py_ssize_t size;
12941293

1295-
if(PyBytes_Check(value)) {
1296-
Py_INCREF(value);
1297-
} else {
1294+
if(!PyBytes_Check(value)) {
12981295
PyErr_Format(PyExc_TypeError,
12991296
"expected bytes, %s found",
13001297
value->ob_type->tp_name);
13011298
return NULL;
13021299
}
13031300

13041301
data = PyBytes_AS_STRING(value);
1305-
if (!data)
1306-
return NULL;
13071302
size = strlen(data); /* XXX Why not Py_SIZE(value)? */
13081303
if (size < length) {
1309-
/* This will copy the leading NUL character
1304+
/* This will copy the terminating NUL character
13101305
* if there is space for it.
13111306
*/
13121307
++size;
13131308
} else if (size > length) {
13141309
PyErr_Format(PyExc_ValueError,
13151310
"bytes too long (%zd, maximum length %zd)",
13161311
size, length);
1317-
Py_DECREF(value);
13181312
return NULL;
13191313
}
13201314
/* Also copy the terminating NUL character if there is space */
13211315
memcpy((char *)ptr, data, size);
13221316

1323-
Py_DECREF(value);
13241317
_RET(value);
13251318
}
13261319

@@ -1428,9 +1421,7 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)
14281421
/* convert value into a PyUnicodeObject or NULL */
14291422
if (Py_None == value) {
14301423
value = NULL;
1431-
} else if (PyUnicode_Check(value)) {
1432-
Py_INCREF(value); /* for the descref below */
1433-
} else {
1424+
} else if (!PyUnicode_Check(value)) {
14341425
PyErr_Format(PyExc_TypeError,
14351426
"unicode string expected instead of %s instance",
14361427
value->ob_type->tp_name);
@@ -1449,7 +1440,6 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)
14491440
return NULL;
14501441
}
14511442
bstr = SysAllocStringLen(wvalue, (unsigned)wsize);
1452-
Py_DECREF(value);
14531443
} else
14541444
bstr = NULL;
14551445

0 commit comments

Comments
 (0)