diff -r f9dd607dc04c Modules/_datetimemodule.c --- a/Modules/_datetimemodule.c Tue Jan 03 02:01:42 2017 +0100 +++ b/Modules/_datetimemodule.c Tue Jan 03 14:58:16 2017 +0100 @@ -120,6 +120,8 @@ static PyTypeObject PyDateTime_TimeType; static PyTypeObject PyDateTime_TZInfoType; static PyTypeObject PyDateTime_TimeZoneType; +static int check_tzinfo_subclass(PyObject *p); + _Py_IDENTIFIER(as_integer_ratio); _Py_IDENTIFIER(fromutc); _Py_IDENTIFIER(isoformat); @@ -672,6 +674,10 @@ new_date_ex(int year, int month, int day { PyDateTime_Date *self; + if (check_date_args(year, month, day) < 0) { + return NULL; + } + self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); if (self != NULL) set_date_fields(self, year, month, day); @@ -689,6 +695,16 @@ new_datetime_ex2(int year, int month, in PyDateTime_DateTime *self; char aware = tzinfo != Py_None; + if (check_date_args(year, month, day) < 0) { + return NULL; + } + if (check_time_args(hour, minute, second, usecond, fold) < 0) { + return NULL; + } + if (check_tzinfo_subclass(tzinfo) < 0) { + return NULL; + } + self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); if (self != NULL) { self->hastzinfo = aware; @@ -726,6 +742,13 @@ new_time_ex2(int hour, int minute, int s PyDateTime_Time *self; char aware = tzinfo != Py_None; + if (check_time_args(hour, minute, second, usecond, fold) < 0) { + return NULL; + } + if (check_tzinfo_subclass(tzinfo) < 0) { + return NULL; + } + self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); if (self != NULL) { self->hastzinfo = aware; @@ -2511,8 +2534,6 @@ date_new(PyTypeObject *type, PyObject *a if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, &year, &month, &day)) { - if (check_date_args(year, month, day) < 0) - return NULL; self = new_date_ex(year, month, day, type); } return self; @@ -3598,10 +3619,6 @@ time_new(PyTypeObject *type, PyObject *a if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, &hour, &minute, &second, &usecond, &tzinfo, &fold)) { - if (check_time_args(hour, minute, second, usecond, fold) < 0) - return NULL; - if (check_tzinfo_subclass(tzinfo) < 0) - return NULL; self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, type); } @@ -4188,12 +4205,6 @@ datetime_new(PyTypeObject *type, PyObjec if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, &year, &month, &day, &hour, &minute, &second, &usecond, &tzinfo, &fold)) { - if (check_date_args(year, month, day) < 0) - return NULL; - if (check_time_args(hour, minute, second, usecond, fold) < 0) - return NULL; - if (check_tzinfo_subclass(tzinfo) < 0) - return NULL; self = new_datetime_ex2(year, month, day, hour, minute, second, usecond, tzinfo, fold, type); @@ -4269,6 +4280,13 @@ datetime_from_timet_and_us(PyObject *cls */ second = Py_MIN(59, tm.tm_sec); + /* reject too old or too recent date to avoid overflow + when computing fold */ + if (check_date_args(year, month, day) < 0) { + return NULL; + } + + /* local timezone requires to compute fold */ if (tzinfo == Py_None && f == _PyTime_localtime) { long long probe_seconds, result_seconds, transition; @@ -4528,12 +4546,13 @@ add_datetime_timedelta(PyDateTime_DateTi assert(factor == 1 || factor == -1); if (normalize_datetime(&year, &month, &day, - &hour, &minute, &second, µsecond) < 0) + &hour, &minute, &second, µsecond) < 0) { return NULL; - else - return new_datetime(year, month, day, - hour, minute, second, microsecond, - HASTZINFO(date) ? date->tzinfo : Py_None, 0); + } + + return new_datetime(year, month, day, + hour, minute, second, microsecond, + HASTZINFO(date) ? date->tzinfo : Py_None, 0); } static PyObject *