Skip to content

Commit bc23ad4

Browse files
[3.12] bpo-36796: Clean the error handling in _testcapimodule.c (GH-13085) (GH-113132)
(cherry picked from commit a723a13) Co-authored-by: Zackery Spytz <[email protected]>
1 parent 1511451 commit bc23ad4

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

Modules/_testcapi/datetime.c

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,48 +85,78 @@ make_timezones_capi(PyObject *self, PyObject *args)
8585
{
8686
PyObject *offset = PyDelta_FromDSU(0, -18000, 0);
8787
PyObject *name = PyUnicode_FromString("EST");
88+
if (offset == NULL || name == NULL) {
89+
Py_XDECREF(offset);
90+
Py_XDECREF(name);
91+
return NULL;
92+
}
8893

8994
PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name);
9095
PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name);
9196
PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset);
92-
93-
Py_DecRef(offset);
94-
Py_DecRef(name);
95-
97+
Py_DECREF(offset);
98+
Py_DECREF(name);
99+
if (est_zone_capi == NULL || est_zone_macro == NULL ||
100+
est_zone_macro_noname == NULL)
101+
{
102+
goto error;
103+
}
96104
PyObject *rv = PyTuple_New(3);
97105
if (rv == NULL) {
98-
return NULL;
106+
goto error;
99107
}
100108

101109
PyTuple_SET_ITEM(rv, 0, est_zone_capi);
102110
PyTuple_SET_ITEM(rv, 1, est_zone_macro);
103111
PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname);
104112

105113
return rv;
114+
error:
115+
Py_XDECREF(est_zone_capi);
116+
Py_XDECREF(est_zone_macro);
117+
Py_XDECREF(est_zone_macro_noname);
118+
return NULL;
106119
}
107120

108121
static PyObject *
109122
get_timezones_offset_zero(PyObject *self, PyObject *args)
110123
{
111124
PyObject *offset = PyDelta_FromDSU(0, 0, 0);
112125
PyObject *name = PyUnicode_FromString("");
126+
if (offset == NULL || name == NULL) {
127+
Py_XDECREF(offset);
128+
Py_XDECREF(name);
129+
return NULL;
130+
}
113131

114132
// These two should return the UTC singleton
115133
PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
116134
PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL);
117135

118136
// This one will return +00:00 zone, but not the UTC singleton
119137
PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name);
120-
121-
Py_DecRef(offset);
122-
Py_DecRef(name);
138+
Py_DECREF(offset);
139+
Py_DECREF(name);
140+
if (utc_singleton_0 == NULL || utc_singleton_1 == NULL ||
141+
non_utc_zone == NULL)
142+
{
143+
goto error;
144+
}
123145

124146
PyObject *rv = PyTuple_New(3);
147+
if (rv == NULL) {
148+
goto error;
149+
}
125150
PyTuple_SET_ITEM(rv, 0, utc_singleton_0);
126151
PyTuple_SET_ITEM(rv, 1, utc_singleton_1);
127152
PyTuple_SET_ITEM(rv, 2, non_utc_zone);
128153

129154
return rv;
155+
error:
156+
Py_XDECREF(utc_singleton_0);
157+
Py_XDECREF(utc_singleton_1);
158+
Py_XDECREF(non_utc_zone);
159+
return NULL;
130160
}
131161

132162
static PyObject *

Modules/_testcapimodule.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ test_dict_inner(int count)
212212
for (i = 0; i < count; i++) {
213213
v = PyLong_FromLong(i);
214214
if (v == NULL) {
215-
return -1;
215+
goto error;
216216
}
217217
if (PyDict_SetItem(dict, v, v) < 0) {
218218
Py_DECREF(v);
219-
return -1;
219+
goto error;
220220
}
221221
Py_DECREF(v);
222222
}
@@ -230,11 +230,12 @@ test_dict_inner(int count)
230230
assert(v != UNINITIALIZED_PTR);
231231
i = PyLong_AS_LONG(v) + 1;
232232
o = PyLong_FromLong(i);
233-
if (o == NULL)
234-
return -1;
233+
if (o == NULL) {
234+
goto error;
235+
}
235236
if (PyDict_SetItem(dict, k, o) < 0) {
236237
Py_DECREF(o);
237-
return -1;
238+
goto error;
238239
}
239240
Py_DECREF(o);
240241
k = v = UNINITIALIZED_PTR;
@@ -252,6 +253,9 @@ test_dict_inner(int count)
252253
} else {
253254
return 0;
254255
}
256+
error:
257+
Py_DECREF(dict);
258+
return -1;
255259
}
256260

257261

@@ -1700,7 +1704,9 @@ test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
17001704
descr.n_in_sequence = 1;
17011705

17021706
PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
1703-
assert(structseq_type != NULL);
1707+
if (structseq_type == NULL) {
1708+
return NULL;
1709+
}
17041710
assert(PyType_Check(structseq_type));
17051711
assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
17061712
Py_DECREF(structseq_type);

0 commit comments

Comments
 (0)