]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-111295: Fix error checking in time extension module init (GH-111296) (...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 1 Nov 2023 19:58:02 +0000 (20:58 +0100)
committerGitHub <noreply@github.com>
Wed, 1 Nov 2023 19:58:02 +0000 (20:58 +0100)
gh-111295: Fix error checking in time extension module init (GH-111296)

Introduce ADD_INT macro wrapper for PyModule_AddIntConstant()
(cherry picked from commit 81b03e78101c97c1d3fe5f90908bbf94e83d7df1)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Misc/NEWS.d/next/Library/2023-10-25-08-42-05.gh-issue-111295.H2K4lf.rst [new file with mode: 0644]
Modules/timemodule.c

diff --git a/Misc/NEWS.d/next/Library/2023-10-25-08-42-05.gh-issue-111295.H2K4lf.rst b/Misc/NEWS.d/next/Library/2023-10-25-08-42-05.gh-issue-111295.H2K4lf.rst
new file mode 100644 (file)
index 0000000..28b85ec
--- /dev/null
@@ -0,0 +1 @@
+Fix :mod:`time` not checking for errors when initializing.
index f5b0f39e14abc3bfc3887da9ce119c643aec3b26..3b46deacdf2de29b781b9055a7ad15ffda8b392f 100644 (file)
@@ -1737,6 +1737,12 @@ get_gmtoff(time_t t, struct tm *p)
 static int
 init_timezone(PyObject *m)
 {
+#define ADD_INT(NAME, VALUE) do {                       \
+    if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) {  \
+        return -1;                                      \
+    }                                                   \
+} while (0)
+
     assert(!PyErr_Occurred());
 
     /* This code moved from PyInit_time wholesale to allow calling it from
@@ -1760,13 +1766,13 @@ init_timezone(PyObject *m)
 #if !defined(MS_WINDOWS) || defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
     tzset();
 #endif
-    PyModule_AddIntConstant(m, "timezone", _Py_timezone);
+    ADD_INT("timezone", _Py_timezone);
 #ifdef HAVE_ALTZONE
-    PyModule_AddIntConstant(m, "altzone", altzone);
+    ADD_INT("altzone", altzone);
 #else
-    PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600);
+    ADD_INT("altzone", _Py_timezone-3600);
 #endif
-    PyModule_AddIntConstant(m, "daylight", _Py_daylight);
+    ADD_INT("daylight", _Py_daylight);
 #ifdef MS_WINDOWS
     TIME_ZONE_INFORMATION tzinfo = {0};
     GetTimeZoneInformation(&tzinfo);
@@ -1825,20 +1831,21 @@ init_timezone(PyObject *m)
     PyObject *tzname_obj;
     if (janzone < julyzone) {
         /* DST is reversed in the southern hemisphere */
-        PyModule_AddIntConstant(m, "timezone", julyzone);
-        PyModule_AddIntConstant(m, "altzone", janzone);
-        PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
+        ADD_INT("timezone", julyzone);
+        ADD_INT("altzone", janzone);
+        ADD_INT("daylight", janzone != julyzone);
         tzname_obj = Py_BuildValue("(zz)", julyname, janname);
     } else {
-        PyModule_AddIntConstant(m, "timezone", janzone);
-        PyModule_AddIntConstant(m, "altzone", julyzone);
-        PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
+        ADD_INT("timezone", janzone);
+        ADD_INT("altzone", julyzone);
+        ADD_INT("daylight", janzone != julyzone);
         tzname_obj = Py_BuildValue("(zz)", janname, julyname);
     }
     if (_PyModule_Add(m, "tzname", tzname_obj) < 0) {
         return -1;
     }
 #endif // !HAVE_DECL_TZNAME
+#undef ADD_INT
 
     if (PyErr_Occurred()) {
         return -1;