@@ -123,6 +123,9 @@ static const int SOURCE_FILE = 2;
123123
124124static const size_t ZONEINFO_STRONG_CACHE_MAX_SIZE = 8 ;
125125
126+ static const int MINYEAR = 1 ;
127+ static const int MAXYEAR = 9999 ;
128+
126129// Forward declarations
127130static int
128131load_data (zoneinfo_state * state , PyZoneInfo_ZoneInfo * self ,
@@ -2200,17 +2203,24 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
22002203 unsigned char fold ;
22012204 if (PyDateTime_Check (dt )) {
22022205 fold = PyDateTime_DATE_GET_FOLD (dt );
2203- } else {
2206+ }
2207+ else {
22042208 PyObject * fold_obj = PyObject_GetAttrString (dt , "fold" );
22052209 if (fold_obj == NULL ) {
22062210 return NULL ;
22072211 }
22082212
2209- fold = ( unsigned char ) PyLong_AsLong (fold_obj );
2213+ long fold_int = PyLong_AsLong (fold_obj );
22102214 Py_DECREF (fold_obj );
22112215 if (PyErr_Occurred ()) {
22122216 return NULL ;
22132217 }
2218+ if (fold_int < 0 || fold_int > 2 ) {
2219+ PyErr_Format (PyExc_ValueError ,
2220+ "fold must be 0 or 1, got %d" , fold_int );
2221+ return NULL ;
2222+ }
2223+ fold = (unsigned char )fold_int ;
22142224 }
22152225
22162226 assert (fold < 2 );
@@ -2224,20 +2234,32 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
22242234 int year ;
22252235 if (PyDateTime_Check (dt )) {
22262236 year = PyDateTime_GET_YEAR (dt );
2227- } else {
2237+ }
2238+ else {
22282239 PyObject * year_obj = PyObject_GetAttrString (dt , "year" );
22292240 if (year_obj == NULL ) {
22302241 return NULL ;
22312242 }
22322243
2233- year = PyLong_AsLong (year_obj );
2244+ year = PyLong_AsInt (year_obj );
22342245 Py_DECREF (year_obj );
22352246 if (PyErr_Occurred ()) {
22362247 return NULL ;
22372248 }
2249+
2250+ if (year < MINYEAR || year > 9999 ) {
2251+ PyErr_Format (PyExc_ValueError ,
2252+ "year out of range, should be in (%d, %d) but got %d" ,
2253+ MINYEAR ,
2254+ MAXYEAR ,
2255+ year
2256+ );
2257+ return NULL ;
2258+ }
22382259 }
22392260 return find_tzrule_ttinfo (& (self -> tzrule_after ), ts , fold , year );
2240- } else {
2261+ }
2262+ else {
22412263 size_t idx = _bisect (ts , local_transitions , self -> num_transitions ) - 1 ;
22422264 assert (idx < self -> num_transitions );
22432265 return self -> trans_ttinfos [idx ];
0 commit comments