Skip to content

Commit 85889e5

Browse files
StanFromIrelandmiss-islington
authored andcommitted
Fix a dangling pointer and reference leak in local_timezone_from_timestamp (GH-156598)
(cherry picked from commit fcf0a98) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 0ee064f commit 85889e5

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

Modules/_datetimemodule.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6588,6 +6588,9 @@ local_timezone_from_timestamp(time_t timestamp)
65886588
struct tm local_time_tm;
65896589
PyObject *nameo = NULL;
65906590
const char *zone = NULL;
6591+
#ifndef HAVE_STRUCT_TM_TM_ZONE
6592+
char buf[100]; // for zone, which is used after the block below
6593+
#endif
65916594

65926595
if (_PyTime_localtime(timestamp, &local_time_tm) != 0)
65936596
return NULL;
@@ -6598,9 +6601,9 @@ local_timezone_from_timestamp(time_t timestamp)
65986601
{
65996602
PyObject *local_time, *utc_time;
66006603
struct tm utc_time_tm;
6601-
char buf[100];
6602-
strftime(buf, sizeof(buf), "%Z", &local_time_tm);
6603-
zone = buf;
6604+
if (strftime(buf, sizeof(buf), "%Z", &local_time_tm) != 0) {
6605+
zone = buf;
6606+
}
66046607
local_time = new_datetime(local_time_tm.tm_year + 1900,
66056608
local_time_tm.tm_mon + 1,
66066609
local_time_tm.tm_mday,
@@ -6610,8 +6613,10 @@ local_timezone_from_timestamp(time_t timestamp)
66106613
if (local_time == NULL) {
66116614
return NULL;
66126615
}
6613-
if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0)
6616+
if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) {
6617+
Py_DECREF(local_time);
66146618
return NULL;
6619+
}
66156620
utc_time = new_datetime(utc_time_tm.tm_year + 1900,
66166621
utc_time_tm.tm_mon + 1,
66176622
utc_time_tm.tm_mday,

0 commit comments

Comments
 (0)