]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] bpo-36796: Clean the error handling in _testcapimodule.c (GH-13085) (GH-113132)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 14 Dec 2023 19:39:57 +0000 (20:39 +0100)
committerGitHub <noreply@github.com>
Thu, 14 Dec 2023 19:39:57 +0000 (19:39 +0000)
(cherry picked from commit a723a13bf135306cdc5999a959596bfb487e8f4f)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
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 b1cb9350e5984c0ec00185caa579b9607801f00b..16eb881dbd23440d2ceb64e88b94189a6f432479 100644 (file)
@@ -212,11 +212,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);
     }
@@ -230,11 +230,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;
@@ -252,6 +253,9 @@ test_dict_inner(int count)
     } else {
         return 0;
     }
+error:
+    Py_DECREF(dict);
+    return -1;
 }
 
 
@@ -1700,7 +1704,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);