Skip to content

Commit f366a80

Browse files
committed
Prevent segfaults with zoneinfo and non-datetime
1 parent 1f720b1 commit f366a80

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

‎Modules/_zoneinfo.c‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,22 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
22352235
return NULL;
22362236
}
22372237

2238-
unsigned char fold = PyDateTime_DATE_GET_FOLD(dt);
2238+
unsigned char fold;
2239+
if (PyDateTime_Check(dt)) {
2240+
fold = PyDateTime_DATE_GET_FOLD(dt);
2241+
} else {
2242+
PyObject *fold_obj = PyObject_GetAttrString(dt, "fold");
2243+
if (fold_obj == NULL) {
2244+
return NULL;
2245+
}
2246+
2247+
fold = (unsigned char)PyLong_AsLong(fold_obj);
2248+
Py_DECREF(fold_obj);
2249+
if (PyErr_Occurred()) {
2250+
return NULL;
2251+
}
2252+
}
2253+
22392254
assert(fold < 2);
22402255
int64_t *local_transitions = self->trans_list_wall[fold];
22412256
size_t num_trans = self->num_transitions;
@@ -2244,10 +2259,23 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
22442259
return self->ttinfo_before;
22452260
}
22462261
else if (!num_trans || ts > local_transitions[self->num_transitions - 1]) {
2247-
return find_tzrule_ttinfo(&(self->tzrule_after), ts, fold,
2248-
PyDateTime_GET_YEAR(dt));
2249-
}
2250-
else {
2262+
int year;
2263+
if (PyDateTime_Check(dt)) {
2264+
year = PyDateTime_GET_YEAR(dt);
2265+
} else {
2266+
PyObject *year_obj = PyObject_GetAttrString(dt, "year");
2267+
if (year_obj == NULL) {
2268+
return NULL;
2269+
}
2270+
2271+
year = PyLong_AsLong(year_obj);
2272+
Py_DECREF(year_obj);
2273+
if (PyErr_Occurred()) {
2274+
return NULL;
2275+
}
2276+
}
2277+
return find_tzrule_ttinfo(&(self->tzrule_after), ts, fold, year);
2278+
} else {
22512279
size_t idx = _bisect(ts, local_transitions, self->num_transitions) - 1;
22522280
assert(idx < self->num_transitions);
22532281
return self->trans_ttinfos[idx];

0 commit comments

Comments
 (0)