Skip to content

Commit d978ab5

Browse files
Move cleanup of locals to error section
1 parent a4f8468 commit d978ab5

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

Modules/_ctypes/stgdict.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,17 @@ MakeAnonFields(PyObject *type)
217217
int
218218
PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct)
219219
{
220-
PyObject *tmp;
221220
Py_ssize_t ffi_ofs;
222221
int arrays_seen = 0;
223222

224223
int retval = -1;
225224
// The following are NULL or hold strong references.
226225
// They're cleared on error.
226+
PyObject *layout_func = NULL;
227+
PyObject *kwnames = NULL;
228+
PyObject* align = NULL;
229+
PyObject* size = NULL;
230+
PyObject *layout_fields_obj = NULL;
227231
PyObject *layout_fields = NULL;
228232
PyObject *layout = NULL;
229233
PyObject *format_spec_obj = NULL;
@@ -257,17 +261,15 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
257261
goto error;
258262
}
259263

260-
PyObject *layout_func = PyImport_ImportModuleAttrString("ctypes._layout",
261-
"get_layout");
264+
layout_func = PyImport_ImportModuleAttrString("ctypes._layout", "get_layout");
262265
if (!layout_func) {
263266
goto error;
264267
}
265-
PyObject *kwnames = PyTuple_Pack(
268+
kwnames = PyTuple_Pack(
266269
2,
267270
&_Py_ID(is_struct),
268271
&_Py_ID(base));
269272
if (!kwnames) {
270-
Py_DECREF(layout_func);
271273
goto error;
272274
}
273275
layout = PyObject_Vectorcall(
@@ -282,19 +284,19 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
282284
baseinfo ? base : Py_None},
283285
2 | PY_VECTORCALL_ARGUMENTS_OFFSET,
284286
kwnames);
285-
Py_DECREF(kwnames);
286-
Py_DECREF(layout_func);
287+
Py_CLEAR(kwnames);
288+
Py_CLEAR(layout_func);
287289
fields = NULL; // a borrowed reference we won't be using again
288290
if (!layout) {
289291
goto error;
290292
}
291293

292-
tmp = PyObject_GetAttr(layout, &_Py_ID(align));
293-
if (!tmp) {
294+
align = PyObject_GetAttr(layout, &_Py_ID(align));
295+
if (!align) {
294296
goto error;
295297
}
296-
Py_ssize_t total_align = PyLong_AsSsize_t(tmp);
297-
Py_DECREF(tmp);
298+
Py_ssize_t total_align = PyLong_AsSsize_t(align);
299+
Py_CLEAR(align);
298300
if (total_align < 0) {
299301
if (!PyErr_Occurred()) {
300302
PyErr_SetString(PyExc_ValueError,
@@ -303,12 +305,12 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
303305
goto error;
304306
}
305307

306-
tmp = PyObject_GetAttr(layout, &_Py_ID(size));
307-
if (!tmp) {
308+
size = PyObject_GetAttr(layout, &_Py_ID(size));
309+
if (!size) {
308310
goto error;
309311
}
310-
Py_ssize_t total_size = PyLong_AsSsize_t(tmp);
311-
Py_DECREF(tmp);
312+
Py_ssize_t total_size = PyLong_AsSsize_t(size);
313+
Py_CLEAR(size);
312314
if (total_size < 0) {
313315
if (!PyErr_Occurred()) {
314316
PyErr_SetString(PyExc_ValueError,
@@ -339,15 +341,15 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
339341
}
340342
memcpy(stginfo->format, format_spec, format_spec_size + 1);
341343

342-
PyObject *layout_fields_obj = PyObject_GetAttr(layout, &_Py_ID(fields));
344+
layout_fields_obj = PyObject_GetAttr(layout, &_Py_ID(fields));
343345
if (!layout_fields_obj) {
344346
goto error;
345347
}
346348
layout_fields = PySequence_Tuple(layout_fields_obj);
347-
Py_DECREF(layout_fields_obj);
348349
if (!layout_fields) {
349350
goto error;
350351
}
352+
Py_CLEAR(layout_fields_obj);
351353
Py_CLEAR(layout);
352354

353355
Py_ssize_t len = PyTuple_GET_SIZE(layout_fields);
@@ -665,6 +667,11 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
665667

666668
retval = MakeAnonFields(type);
667669
error:
670+
Py_XDECREF(layout_func);
671+
Py_XDECREF(kwnames);
672+
Py_XDECREF(align);
673+
Py_XDECREF(size);
674+
Py_XDECREF(layout_fields_obj);
668675
Py_XDECREF(layout_fields);
669676
Py_XDECREF(layout);
670677
Py_XDECREF(format_spec_obj);

0 commit comments

Comments
 (0)