]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36796: Clean the error handling in _testcapimodule.c (GH-13085)
authorZackery Spytz <zspytz@gmail.com>
Thu, 14 Dec 2023 19:06:53 +0000 (11:06 -0800)
committerGitHub <noreply@github.com>
Thu, 14 Dec 2023 19:06:53 +0000 (19:06 +0000)
Modules/_testcapi/datetime.c
Modules/_testcapimodule.c

index 88f992915fa8c10445db303eef7ee69d1331dc9e..b1796039f0d83af8b945189d9387ec1ee082ffa0 100644 (file)
@@ -85,17 +85,25 @@ make_timezones_capi(PyObject *self, PyObject *args)
 {
     PyObject *offset = PyDelta_FromDSU(0, -18000, 0);
     PyObject *name = PyUnicode_FromString("EST");
+    if (offset == NULL || name == NULL) {
+        Py_XDECREF(offset);
+        Py_XDECREF(name);
+        return NULL;
+    }
 
     PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name);
     PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name);
     PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset);
-
-    Py_DecRef(offset);
-    Py_DecRef(name);
-
+    Py_DECREF(offset);
+    Py_DECREF(name);
+    if (est_zone_capi == NULL || est_zone_macro == NULL ||
+        est_zone_macro_noname == NULL)
+    {
+        goto error;
+    }
     PyObject *rv = PyTuple_New(3);
     if (rv == NULL) {
-        return NULL;
+        goto error;
     }
 
     PyTuple_SET_ITEM(rv, 0, est_zone_capi);
@@ -103,6 +111,11 @@ make_timezones_capi(PyObject *self, PyObject *args)
     PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname);
 
     return rv;
+error:
+    Py_XDECREF(est_zone_capi);
+    Py_XDECREF(est_zone_macro);
+    Py_XDECREF(est_zone_macro_noname);
+    return NULL;
 }
 
 static PyObject *
@@ -110,6 +123,11 @@ get_timezones_offset_zero(PyObject *self, PyObject *args)
 {
     PyObject *offset = PyDelta_FromDSU(0, 0, 0);
     PyObject *name = PyUnicode_FromString("");
+    if (offset == NULL || name == NULL) {
+        Py_XDECREF(offset);
+        Py_XDECREF(name);
+        return NULL;
+    }
 
     // These two should return the UTC singleton
     PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
@@ -117,16 +135,28 @@ get_timezones_offset_zero(PyObject *self, PyObject *args)
 
     // This one will return +00:00 zone, but not the UTC singleton
     PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name);
-
-    Py_DecRef(offset);
-    Py_DecRef(name);
+    Py_DECREF(offset);
+    Py_DECREF(name);
+    if (utc_singleton_0 == NULL || utc_singleton_1 == NULL ||
+        non_utc_zone == NULL)
+    {
+        goto error;
+    }
 
     PyObject *rv = PyTuple_New(3);
+    if (rv == NULL) {
+        goto error;
+    }
     PyTuple_SET_ITEM(rv, 0, utc_singleton_0);
     PyTuple_SET_ITEM(rv, 1, utc_singleton_1);
     PyTuple_SET_ITEM(rv, 2, non_utc_zone);
 
     return rv;
+error:
+    Py_XDECREF(utc_singleton_0);
+    Py_XDECREF(utc_singleton_1);
+    Py_XDECREF(non_utc_zone);
+    return NULL;
 }
 
 static PyObject *
index b3ddfae58e6fc001ed79026086105b0e6b7d5047..3527dfa77279ac8921cc67ca2649bdffd04c55ef 100644 (file)
@@ -196,11 +196,11 @@ test_dict_inner(int count)
     for (i = 0; i < count; i++) {
         v = PyLong_FromLong(i);
         if (v == NULL) {
-            return -1;
+            goto error;
         }
         if (PyDict_SetItem(dict, v, v) < 0) {
             Py_DECREF(v);
-            return -1;
+            goto error;
         }
         Py_DECREF(v);
     }
@@ -214,11 +214,12 @@ test_dict_inner(int count)
         assert(v != UNINITIALIZED_PTR);
         i = PyLong_AS_LONG(v) + 1;
         o = PyLong_FromLong(i);
-        if (o == NULL)
-            return -1;
+        if (o == NULL) {
+            goto error;
+        }
         if (PyDict_SetItem(dict, k, o) < 0) {
             Py_DECREF(o);
-            return -1;
+            goto error;
         }
         Py_DECREF(o);
         k = v = UNINITIALIZED_PTR;
@@ -236,6 +237,9 @@ test_dict_inner(int count)
     } else {
         return 0;
     }
+error:
+    Py_DECREF(dict);
+    return -1;
 }
 
 
@@ -1556,7 +1560,9 @@ test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
     descr.n_in_sequence = 1;
 
     PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
-    assert(structseq_type != NULL);
+    if (structseq_type == NULL) {
+        return NULL;
+    }
     assert(PyType_Check(structseq_type));
     assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
     Py_DECREF(structseq_type);