[3.12] gh-111295: Fix error checking in time extension module init (GH-111296) (#111300)

gh-111295: Fix error checking in time extension module init (GH-111296)

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

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2023-11-01 20:58:02 +01:00 committed by GitHub
parent f1087855e2
commit a94bdc2459
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View file

@ -0,0 +1 @@
Fix :mod:`time` not checking for errors when initializing.

View file

@ -1737,6 +1737,12 @@ get_gmtoff(time_t t, struct tm *p)
static int static int
init_timezone(PyObject *m) init_timezone(PyObject *m)
{ {
#define ADD_INT(NAME, VALUE) do { \
if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) { \
return -1; \
} \
} while (0)
assert(!PyErr_Occurred()); assert(!PyErr_Occurred());
/* This code moved from PyInit_time wholesale to allow calling it from /* 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) #if !defined(MS_WINDOWS) || defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
tzset(); tzset();
#endif #endif
PyModule_AddIntConstant(m, "timezone", _Py_timezone); ADD_INT("timezone", _Py_timezone);
#ifdef HAVE_ALTZONE #ifdef HAVE_ALTZONE
PyModule_AddIntConstant(m, "altzone", altzone); ADD_INT("altzone", altzone);
#else #else
PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600); ADD_INT("altzone", _Py_timezone-3600);
#endif #endif
PyModule_AddIntConstant(m, "daylight", _Py_daylight); ADD_INT("daylight", _Py_daylight);
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
TIME_ZONE_INFORMATION tzinfo = {0}; TIME_ZONE_INFORMATION tzinfo = {0};
GetTimeZoneInformation(&tzinfo); GetTimeZoneInformation(&tzinfo);
@ -1825,20 +1831,21 @@ init_timezone(PyObject *m)
PyObject *tzname_obj; PyObject *tzname_obj;
if (janzone < julyzone) { if (janzone < julyzone) {
/* DST is reversed in the southern hemisphere */ /* DST is reversed in the southern hemisphere */
PyModule_AddIntConstant(m, "timezone", julyzone); ADD_INT("timezone", julyzone);
PyModule_AddIntConstant(m, "altzone", janzone); ADD_INT("altzone", janzone);
PyModule_AddIntConstant(m, "daylight", janzone != julyzone); ADD_INT("daylight", janzone != julyzone);
tzname_obj = Py_BuildValue("(zz)", julyname, janname); tzname_obj = Py_BuildValue("(zz)", julyname, janname);
} else { } else {
PyModule_AddIntConstant(m, "timezone", janzone); ADD_INT("timezone", janzone);
PyModule_AddIntConstant(m, "altzone", julyzone); ADD_INT("altzone", julyzone);
PyModule_AddIntConstant(m, "daylight", janzone != julyzone); ADD_INT("daylight", janzone != julyzone);
tzname_obj = Py_BuildValue("(zz)", janname, julyname); tzname_obj = Py_BuildValue("(zz)", janname, julyname);
} }
if (_PyModule_Add(m, "tzname", tzname_obj) < 0) { if (_PyModule_Add(m, "tzname", tzname_obj) < 0) {
return -1; return -1;
} }
#endif // !HAVE_DECL_TZNAME #endif // !HAVE_DECL_TZNAME
#undef ADD_INT
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
return -1; return -1;