Skip to content

Commit c595424

Browse files
Fix a dangling pointer and reference leak in local_timezone_from_timestamp
1 parent b0da7c7 commit c595424

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎Modules/_datetimemodule.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6761,6 +6761,9 @@ local_timezone_from_timestamp(time_t timestamp)
67616761
struct tm local_time_tm;
67626762
PyObject *nameo = NULL;
67636763
const char *zone = NULL;
6764+
#ifndef HAVE_STRUCT_TM_TM_ZONE
6765+
char buf[100];
6766+
#endif
67646767

67656768
if (_PyTime_localtime(timestamp, &local_time_tm) != 0)
67666769
return NULL;
@@ -6771,8 +6774,9 @@ local_timezone_from_timestamp(time_t timestamp)
67716774
{
67726775
PyObject *local_time, *utc_time;
67736776
struct tm utc_time_tm;
6774-
char buf[100];
6775-
strftime(buf, sizeof(buf), "%Z", &local_time_tm);
6777+
if (strftime(buf, sizeof(buf), "%Z", &local_time_tm) == 0) {
6778+
buf[0] = '\0';
6779+
}
67766780
zone = buf;
67776781
local_time = new_datetime(local_time_tm.tm_year + 1900,
67786782
local_time_tm.tm_mon + 1,
@@ -6783,8 +6787,10 @@ local_timezone_from_timestamp(time_t timestamp)
67836787
if (local_time == NULL) {
67846788
return NULL;
67856789
}
6786-
if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0)
6790+
if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) {
6791+
Py_DECREF(local_time);
67876792
return NULL;
6793+
}
67886794
utc_time = new_datetime(utc_time_tm.tm_year + 1900,
67896795
utc_time_tm.tm_mon + 1,
67906796
utc_time_tm.tm_mday,

0 commit comments

Comments
 (0)