Skip to content

Commit 124fd78

Browse files
committed
pythongh-117534: Add checking for input parameter in iso_to_ymd (updated)
1 parent f77aa5b commit 124fd78

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

Lib/test/datetimetester.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,7 @@ def test_fromisoformat_fails(self):
19281928
'2019-W53-1', # No week 53 in 2019
19291929
'2020-W54-1', # No week 54
19301930
'2009\ud80002\ud80028', # Separators are surrogate codepoints
1931+
'0000W25', # Invalid year
19311932
]
19321933

19331934
for bad_str in bad_strs:

Modules/_datetimemodule.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,9 @@ iso_week1_monday(int year)
416416
static int
417417
iso_to_ymd(const int iso_year, const int iso_week, const int iso_day,
418418
int *year, int *month, int *day) {
419-
if (iso_year <= 0) {
420-
return -2;
419+
// Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5)
420+
if (iso_year < MINYEAR || iso_year > MAXYEAR) {
421+
return -4;
421422
}
422423
if (iso_week <= 0 || iso_week >= 53) {
423424
int out_of_range = 1;
@@ -765,7 +766,7 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month,
765766
* -2: Inconsistent date separator usage
766767
* -3: Failed to parse ISO week.
767768
* -4: Failed to parse ISO day.
768-
* -5, -6: Failure in iso_to_ymd
769+
* -5, -6, -7: Failure in iso_to_ymd
769770
*/
770771
const char *p = dtstr;
771772
p = parse_digits(p, year, 4);
@@ -3145,15 +3146,13 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw)
31453146
return NULL;
31463147
}
31473148

3148-
// Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5)
3149-
if (year < MINYEAR || year > MAXYEAR) {
3150-
PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year);
3151-
return NULL;
3152-
}
3153-
31543149
int month;
31553150
int rv = iso_to_ymd(year, week, day, &year, &month, &day);
31563151

3152+
if (rv == -4) {
3153+
PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year);
3154+
return NULL;
3155+
}
31573156

31583157
if (rv == -2) {
31593158
PyErr_Format(PyExc_ValueError, "Invalid week: %d", week);

0 commit comments

Comments
 (0)