Skip to content

Commit 0516f81

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
[2.7] bpo-36421: Fix ref counting bugs in _ctypes.c's PyCArrayType_new(). (GH-12534)
Add missing Py_DECREF()s.
1 parent 469b0a5 commit 0516f81

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15371537
if (length * itemsize < 0) {
15381538
PyErr_SetString(PyExc_OverflowError,
15391539
"array too large");
1540+
Py_DECREF(stgdict);
15401541
return NULL;
15411542
}
15421543

@@ -1559,8 +1560,10 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15591560
/* create the new instance (which is a class,
15601561
since we are a metatype!) */
15611562
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
1562-
if (result == NULL)
1563+
if (result == NULL) {
1564+
Py_DECREF(stgdict);
15631565
return NULL;
1566+
}
15641567

15651568
/* replace the class dict by our updated spam dict */
15661569
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
@@ -1574,12 +1577,16 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15741577
A permanent annoyance: char arrays are also strings!
15751578
*/
15761579
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
1577-
if (-1 == add_getset(result, CharArray_getsets))
1580+
if (-1 == add_getset(result, CharArray_getsets)) {
1581+
Py_DECREF(result);
15781582
return NULL;
1583+
}
15791584
#ifdef CTYPES_UNICODE
15801585
} else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
1581-
if (-1 == add_getset(result, WCharArray_getsets))
1586+
if (-1 == add_getset(result, WCharArray_getsets)) {
1587+
Py_DECREF(result);
15821588
return NULL;
1589+
}
15831590
#endif
15841591
}
15851592

0 commit comments

Comments
 (0)