Skip to content

Commit 2b3a3be

Browse files
committed
Fix various reference-counting bugs found by gcc-cpychecker
1 parent 23c7a30 commit 2b3a3be

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/constants.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Py_Constant_cnew(ftpy_ConstantType *type, unsigned long value)
4848
}
4949
self = (PyLong_Type.tp_new)((PyTypeObject *)type, args, NULL);
5050
if (self == NULL) {
51+
Py_DECREF(args);
5152
return NULL;
5253
}
5354
Py_DECREF(args);
@@ -252,7 +253,6 @@ static int make_constant_type(
252253

253254
if (PyType_Ready(py_constant_type) < 0)
254255
return -1;
255-
Py_INCREF(py_constant_type);
256256

257257
return 0;
258258
}
@@ -285,7 +285,6 @@ static int make_bitflag_type(
285285

286286
if (PyType_Ready(py_constant_type) < 0)
287287
return -1;
288-
Py_INCREF(py_constant_type);
289288

290289
return 0;
291290
}

src/file.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ PyObject* ftpy_PyFile_OpenFile(PyObject *filename, const char *mode)
153153
open = PyDict_GetItemString(builtins, "open");
154154
if (open == NULL) {
155155
Py_DECREF(builtins);
156+
PyErr_SetString(PyExc_AttributeError,
157+
"Internal error: could not get open function");
156158
return NULL;
157159
}
158160
Py_DECREF(builtins);

src/pyutil.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ int ftpy_setup_type(PyObject *module, PyTypeObject *type)
133133
return 0;
134134
} else {
135135
name = qualified_name_to_name(type->tp_name);
136-
return PyModule_AddObject(module, name, (PyObject *)type);
136+
if (PyModule_AddObject(module, name, (PyObject *)type)) {
137+
Py_DECREF(type);
138+
return -1;
139+
}
140+
return 0;
137141
}
138142
}
139143

@@ -193,6 +197,7 @@ PyObject *ftpy_PyBuffer_ToList(PyObject *obj)
193197
list[2] = NULL;
194198

195199
if (PyObject_GetBuffer(obj, &view, 0)) {
200+
PyErr_SetString(PyExc_TypeError, "Could not get buffer");
196201
return NULL;
197202
}
198203

0 commit comments

Comments
 (0)