Skip to content

Commit 62fbe10

Browse files
ZackerySpytzcorona10
authored andcommitted
bpo-36796: Clean the error handling in _testcapimodule.c (pythonGH-13085)
1 parent e6dc5af commit 62fbe10

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
@@ -196,11 +196,11 @@ test_dict_inner(int count)
196196
for (i = 0; i < count; i++) {
197197
v = PyLong_FromLong(i);
198198
if (v == NULL) {
199-
return -1;
199+
goto error;
200200
}
201201
if (PyDict_SetItem(dict, v, v) < 0) {
202202
Py_DECREF(v);
203-
return -1;
203+
goto error;
204204
}
205205
Py_DECREF(v);
206206
}
@@ -214,11 +214,12 @@ test_dict_inner(int count)
214214
assert(v != UNINITIALIZED_PTR);
215215
i = PyLong_AS_LONG(v) + 1;
216216
o = PyLong_FromLong(i);
217-
if (o == NULL)
218-
return -1;
217+
if (o == NULL) {
218+
goto error;
219+
}
219220
if (PyDict_SetItem(dict, k, o) < 0) {
220221
Py_DECREF(o);
221-
return -1;
222+
goto error;
222223
}
223224
Py_DECREF(o);
224225
k = v = UNINITIALIZED_PTR;
@@ -236,6 +237,9 @@ test_dict_inner(int count)
236237
} else {
237238
return 0;
238239
}
240+
error:
241+
Py_DECREF(dict);
242+
return -1;
239243
}
240244

241245

@@ -1556,7 +1560,9 @@ test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
15561560
descr.n_in_sequence = 1;
15571561

15581562
PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
1559-
assert(structseq_type != NULL);
1563+
if (structseq_type == NULL) {
1564+
return NULL;
1565+
}
15601566
assert(PyType_Check(structseq_type));
15611567
assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
15621568
Py_DECREF(structseq_type);

0 commit comments

Comments
 (0)