Skip to content

Commit 81b03e7

Browse files
authored
gh-111295: Fix error checking in time extension module init (#111296)
Introduce ADD_INT macro wrapper for PyModule_AddIntConstant()
1 parent 2838c55 commit 81b03e7

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`time` not checking for errors when initializing.

Modules/timemodule.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,12 @@ get_gmtoff(time_t t, struct tm *p)
17401740
static int
17411741
init_timezone(PyObject *m)
17421742
{
1743+
#define ADD_INT(NAME, VALUE) do { \
1744+
if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) { \
1745+
return -1; \
1746+
} \
1747+
} while (0)
1748+
17431749
assert(!PyErr_Occurred());
17441750

17451751
/* This code moved from PyInit_time wholesale to allow calling it from
@@ -1763,13 +1769,13 @@ init_timezone(PyObject *m)
17631769
#if !defined(MS_WINDOWS) || defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
17641770
tzset();
17651771
#endif
1766-
PyModule_AddIntConstant(m, "timezone", _Py_timezone);
1772+
ADD_INT("timezone", _Py_timezone);
17671773
#ifdef HAVE_ALTZONE
1768-
PyModule_AddIntConstant(m, "altzone", altzone);
1774+
ADD_INT("altzone", altzone);
17691775
#else
1770-
PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600);
1776+
ADD_INT("altzone", _Py_timezone-3600);
17711777
#endif
1772-
PyModule_AddIntConstant(m, "daylight", _Py_daylight);
1778+
ADD_INT("daylight", _Py_daylight);
17731779
#ifdef MS_WINDOWS
17741780
TIME_ZONE_INFORMATION tzinfo = {0};
17751781
GetTimeZoneInformation(&tzinfo);
@@ -1828,20 +1834,21 @@ init_timezone(PyObject *m)
18281834
PyObject *tzname_obj;
18291835
if (janzone < julyzone) {
18301836
/* DST is reversed in the southern hemisphere */
1831-
PyModule_AddIntConstant(m, "timezone", julyzone);
1832-
PyModule_AddIntConstant(m, "altzone", janzone);
1833-
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
1837+
ADD_INT("timezone", julyzone);
1838+
ADD_INT("altzone", janzone);
1839+
ADD_INT("daylight", janzone != julyzone);
18341840
tzname_obj = Py_BuildValue("(zz)", julyname, janname);
18351841
} else {
1836-
PyModule_AddIntConstant(m, "timezone", janzone);
1837-
PyModule_AddIntConstant(m, "altzone", julyzone);
1838-
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
1842+
ADD_INT("timezone", janzone);
1843+
ADD_INT("altzone", julyzone);
1844+
ADD_INT("daylight", janzone != julyzone);
18391845
tzname_obj = Py_BuildValue("(zz)", janname, julyname);
18401846
}
18411847
if (PyModule_Add(m, "tzname", tzname_obj) < 0) {
18421848
return -1;
18431849
}
18441850
#endif // !HAVE_DECL_TZNAME
1851+
#undef ADD_INT
18451852

18461853
if (PyErr_Occurred()) {
18471854
return -1;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy