Skip to content

Commit 9a146ee

Browse files
committed
Issue python#18408: Fix structseq_reduce(), handle PyDict_SetItemString() failure
1 parent e555314 commit 9a146ee

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

Objects/structseq.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ structseq_repr(PyStructSequence *obj)
233233
static PyObject *
234234
structseq_reduce(PyStructSequence* self)
235235
{
236-
PyObject* tup;
237-
PyObject* dict;
236+
PyObject* tup = NULL;
237+
PyObject* dict = NULL;
238238
PyObject* result;
239239
Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields;
240240
int i;
@@ -243,15 +243,12 @@ structseq_reduce(PyStructSequence* self)
243243
n_visible_fields = VISIBLE_SIZE(self);
244244
n_unnamed_fields = UNNAMED_FIELDS(self);
245245
tup = PyTuple_New(n_visible_fields);
246-
if (!tup) {
247-
return NULL;
248-
}
246+
if (!tup)
247+
goto error;
249248

250249
dict = PyDict_New();
251-
if (!dict) {
252-
Py_DECREF(tup);
253-
return NULL;
254-
}
250+
if (!dict)
251+
goto error;
255252

256253
for (i = 0; i < n_visible_fields; i++) {
257254
Py_INCREF(self->ob_item[i]);
@@ -260,8 +257,8 @@ structseq_reduce(PyStructSequence* self)
260257

261258
for (; i < n_fields; i++) {
262259
char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name;
263-
PyDict_SetItemString(dict, n,
264-
self->ob_item[i]);
260+
if (PyDict_SetItemString(dict, n, self->ob_item[i]) < 0)
261+
goto error;
265262
}
266263

267264
result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict);
@@ -270,6 +267,11 @@ structseq_reduce(PyStructSequence* self)
270267
Py_DECREF(dict);
271268

272269
return result;
270+
271+
error:
272+
Py_XDECREF(tup);
273+
Py_XDECREF(dict);
274+
return NULL;
273275
}
274276

275277
static PyMethodDef structseq_methods[] = {

0 commit comments

Comments
 (0)