Commit 9a146eea authored by Victor Stinner's avatar Victor Stinner

Issue #18408: Fix structseq_reduce(), handle PyDict_SetItemString() failure

parent e5553142
...@@ -233,8 +233,8 @@ structseq_repr(PyStructSequence *obj) ...@@ -233,8 +233,8 @@ structseq_repr(PyStructSequence *obj)
static PyObject * static PyObject *
structseq_reduce(PyStructSequence* self) structseq_reduce(PyStructSequence* self)
{ {
PyObject* tup; PyObject* tup = NULL;
PyObject* dict; PyObject* dict = NULL;
PyObject* result; PyObject* result;
Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields; Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields;
int i; int i;
...@@ -243,15 +243,12 @@ structseq_reduce(PyStructSequence* self) ...@@ -243,15 +243,12 @@ structseq_reduce(PyStructSequence* self)
n_visible_fields = VISIBLE_SIZE(self); n_visible_fields = VISIBLE_SIZE(self);
n_unnamed_fields = UNNAMED_FIELDS(self); n_unnamed_fields = UNNAMED_FIELDS(self);
tup = PyTuple_New(n_visible_fields); tup = PyTuple_New(n_visible_fields);
if (!tup) { if (!tup)
return NULL; goto error;
}
dict = PyDict_New(); dict = PyDict_New();
if (!dict) { if (!dict)
Py_DECREF(tup); goto error;
return NULL;
}
for (i = 0; i < n_visible_fields; i++) { for (i = 0; i < n_visible_fields; i++) {
Py_INCREF(self->ob_item[i]); Py_INCREF(self->ob_item[i]);
...@@ -260,8 +257,8 @@ structseq_reduce(PyStructSequence* self) ...@@ -260,8 +257,8 @@ structseq_reduce(PyStructSequence* self)
for (; i < n_fields; i++) { for (; i < n_fields; i++) {
char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name; char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name;
PyDict_SetItemString(dict, n, if (PyDict_SetItemString(dict, n, self->ob_item[i]) < 0)
self->ob_item[i]); goto error;
} }
result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict); result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict);
...@@ -270,6 +267,11 @@ structseq_reduce(PyStructSequence* self) ...@@ -270,6 +267,11 @@ structseq_reduce(PyStructSequence* self)
Py_DECREF(dict); Py_DECREF(dict);
return result; return result;
error:
Py_XDECREF(tup);
Py_XDECREF(dict);
return NULL;
} }
static PyMethodDef structseq_methods[] = { static PyMethodDef structseq_methods[] = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment