From 503ce5c482cb267b0770bc46c315d5cf822bdca9 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 1 Dec 2018 00:39:36 +0100 Subject: get_gmtoff() now returns time_t (GH-10838) get_gmtoff() now returns time_t instead of int to fix the following Visual Studio warning: Modules\timemodule.c(1183): warning C4244: 'return': conversion from 'time_t' to 'int', possible loss of data --- Modules/timemodule.c | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 55b82d7..188f1e6 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -1019,7 +1019,7 @@ of the timezone or altzone attributes on the time module."); #endif /* HAVE_MKTIME */ #ifdef HAVE_WORKING_TZSET -static void PyInit_timezone(PyObject *module); +static int PyInit_timezone(PyObject *module); static PyObject * time_tzset(PyObject *self, PyObject *unused) @@ -1034,7 +1034,9 @@ time_tzset(PyObject *self, PyObject *unused) tzset(); /* Reset timezone, altzone, daylight and tzname */ - PyInit_timezone(m); + if (PyInit_timezone(m) < 0) { + return NULL; + } Py_DECREF(m); if (PyErr_Occurred()) return NULL; @@ -1535,7 +1537,7 @@ get_zone(char *zone, int n, struct tm *p) #endif } -static int +static time_t get_gmtoff(time_t t, struct tm *p) { #ifdef HAVE_STRUCT_TM_TM_ZONE @@ -1546,8 +1548,11 @@ get_gmtoff(time_t t, struct tm *p) } #endif // !HAVE_DECL_TZNAME -static void -PyInit_timezone(PyObject *m) { +static int +PyInit_timezone(PyObject *m) +{ + assert(!PyErr_Occurred()); + /* This code moved from PyInit_time wholesale to allow calling it from time_tzset. In the future, some parts of it can be moved back (for platforms that don't HAVE_WORKING_TZSET, when we know what they @@ -1591,19 +1596,31 @@ PyInit_timezone(PyObject *m) { static const time_t YEAR = (365 * 24 + 6) * 3600; time_t t; struct tm p; - long janzone, julyzone; + time_t janzone_t, julyzone_t; char janname[10], julyname[10]; t = (time((time_t *)0) / YEAR) * YEAR; _PyTime_localtime(t, &p); get_zone(janname, 9, &p); - janzone = -get_gmtoff(t, &p); + janzone_t = -get_gmtoff(t, &p); janname[9] = '\0'; t += YEAR/2; _PyTime_localtime(t, &p); get_zone(julyname, 9, &p); - julyzone = -get_gmtoff(t, &p); + julyzone_t = -get_gmtoff(t, &p); julyname[9] = '\0'; + /* Sanity check, don't check for the validity of timezones. + In practice, it should be more in range -12 hours .. +14 hours. */ +#define MAX_TIMEZONE (48 * 3600) + if (janzone_t < -MAX_TIMEZONE || janzone_t > MAX_TIMEZONE + || julyzone_t < -MAX_TIMEZONE || julyzone_t > MAX_TIMEZONE) + { + PyErr_SetString(PyExc_RuntimeError, "invalid GMT offset"); + return -1; + } + int janzone = (int)janzone_t; + int julyzone = (int)julyzone_t; + PyObject *tzname_obj; if (janzone < julyzone) { /* DST is reversed in the southern hemisphere */ @@ -1617,10 +1634,16 @@ PyInit_timezone(PyObject *m) { PyModule_AddIntConstant(m, "daylight", janzone != julyzone); tzname_obj = Py_BuildValue("(zz)", janname, julyname); } - if (tzname_obj == NULL) - return; + if (tzname_obj == NULL) { + return -1; + } PyModule_AddObject(m, "tzname", tzname_obj); #endif // !HAVE_DECL_TZNAME + + if (PyErr_Occurred()) { + return -1; + } + return 0; } @@ -1721,7 +1744,9 @@ PyInit_time(void) return NULL; /* Set, or reset, module variables like time.timezone */ - PyInit_timezone(m); + if (PyInit_timezone(m) < 0) { + return NULL; + } #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES) -- cgit v0.12