Skip to content

Commit 290e3e5

Browse files
committed
Prevent segfaults with zoneinfo and non-datetime
1 parent fc3544e commit 290e3e5

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

Modules/_zoneinfo.c

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

2200-
unsigned char fold = PyDateTime_DATE_GET_FOLD(dt);
2200+
unsigned char fold;
2201+
if (PyDateTime_Check(dt)) {
2202+
fold = PyDateTime_DATE_GET_FOLD(dt);
2203+
} else {
2204+
PyObject *fold_obj = PyObject_GetAttrString(dt, "fold");
2205+
if (fold_obj == NULL) {
2206+
return NULL;
2207+
}
2208+
2209+
fold = (unsigned char)PyLong_AsLong(fold_obj);
2210+
Py_DECREF(fold_obj);
2211+
if (PyErr_Occurred()) {
2212+
return NULL;
2213+
}
2214+
}
2215+
22012216
assert(fold < 2);
22022217
int64_t *local_transitions = self->trans_list_wall[fold];
22032218
size_t num_trans = self->num_transitions;
@@ -2206,10 +2221,23 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
22062221
return self->ttinfo_before;
22072222
}
22082223
else if (!num_trans || ts > local_transitions[self->num_transitions - 1]) {
2209-
return find_tzrule_ttinfo(&(self->tzrule_after), ts, fold,
2210-
PyDateTime_GET_YEAR(dt));
2211-
}
2212-
else {
2224+
int year;
2225+
if (PyDateTime_Check(dt)) {
2226+
year = PyDateTime_GET_YEAR(dt);
2227+
} else {
2228+
PyObject *year_obj = PyObject_GetAttrString(dt, "year");
2229+
if (year_obj == NULL) {
2230+
return NULL;
2231+
}
2232+
2233+
year = PyLong_AsLong(year_obj);
2234+
Py_DECREF(year_obj);
2235+
if (PyErr_Occurred()) {
2236+
return NULL;
2237+
}
2238+
}
2239+
return find_tzrule_ttinfo(&(self->tzrule_after), ts, fold, year);
2240+
} else {
22132241
size_t idx = _bisect(ts, local_transitions, self->num_transitions) - 1;
22142242
assert(idx < self->num_transitions);
22152243
return self->trans_ttinfos[idx];

0 commit comments

Comments
 (0)