@@ -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 ,
@@ -2238,17 +2241,24 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
22382241 unsigned char fold ;
22392242 if (PyDateTime_Check (dt )) {
22402243 fold = PyDateTime_DATE_GET_FOLD (dt );
2241- } else {
2244+ }
2245+ else {
22422246 PyObject * fold_obj = PyObject_GetAttrString (dt , "fold" );
22432247 if (fold_obj == NULL ) {
22442248 return NULL ;
22452249 }
22462250
2247- fold = ( unsigned char ) PyLong_AsLong (fold_obj );
2251+ long fold_int = PyLong_AsLong (fold_obj );
22482252 Py_DECREF (fold_obj );
22492253 if (PyErr_Occurred ()) {
22502254 return NULL ;
22512255 }
2256+ if (fold_int < 0 || fold_int > 2 ) {
2257+ PyErr_Format (PyExc_ValueError ,
2258+ "fold must be 0 or 1, got %d" , fold_int );
2259+ return NULL ;
2260+ }
2261+ fold = (unsigned char )fold_int ;
22522262 }
22532263
22542264 assert (fold < 2 );
@@ -2262,20 +2272,32 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
22622272 int year ;
22632273 if (PyDateTime_Check (dt )) {
22642274 year = PyDateTime_GET_YEAR (dt );
2265- } else {
2275+ }
2276+ else {
22662277 PyObject * year_obj = PyObject_GetAttrString (dt , "year" );
22672278 if (year_obj == NULL ) {
22682279 return NULL ;
22692280 }
22702281
2271- year = PyLong_AsLong (year_obj );
2282+ year = PyLong_AsInt (year_obj );
22722283 Py_DECREF (year_obj );
22732284 if (PyErr_Occurred ()) {
22742285 return NULL ;
22752286 }
2287+
2288+ if (year < MINYEAR || year > 9999 ) {
2289+ PyErr_Format (PyExc_ValueError ,
2290+ "year out of range, should be in (%d, %d) but got %d" ,
2291+ MINYEAR ,
2292+ MAXYEAR ,
2293+ year
2294+ );
2295+ return NULL ;
2296+ }
22762297 }
22772298 return find_tzrule_ttinfo (& (self -> tzrule_after ), ts , fold , year );
2278- } else {
2299+ }
2300+ else {
22792301 size_t idx = _bisect (ts , local_transitions , self -> num_transitions ) - 1 ;
22802302 assert (idx < self -> num_transitions );
22812303 return self -> trans_ttinfos [idx ];
0 commit comments