From f77aa5bfa3c264484a71143bb729375feaa50c5e Mon Sep 17 00:00:00 2001 From: Vladislav Efanov Date: Thu, 4 Apr 2024 16:04:46 +0300 Subject: [PATCH 01/11] gh-117534: Add checking for input parameter in iso_to_ymd --- Modules/_datetimemodule.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index a626bda2ea9be9..554baefd007f6d 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -416,6 +416,9 @@ iso_week1_monday(int year) static int iso_to_ymd(const int iso_year, const int iso_week, const int iso_day, int *year, int *month, int *day) { + if (iso_year <= 0) { + return -2; + } if (iso_week <= 0 || iso_week >= 53) { int out_of_range = 1; if (iso_week == 53) { From 124fd78aa0c01df47afd7605de3a3e29b185876a Mon Sep 17 00:00:00 2001 From: Vladislav Efanov Date: Fri, 5 Apr 2024 15:10:09 +0300 Subject: [PATCH 02/11] gh-117534: Add checking for input parameter in iso_to_ymd (updated) --- Lib/test/datetimetester.py | 1 + Modules/_datetimemodule.c | 17 ++++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index c77263998c99f5..43b5e6fb43b888 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -1928,6 +1928,7 @@ def test_fromisoformat_fails(self): '2019-W53-1', # No week 53 in 2019 '2020-W54-1', # No week 54 '2009\ud80002\ud80028', # Separators are surrogate codepoints + '0000W25', # Invalid year ] for bad_str in bad_strs: diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 554baefd007f6d..2c9ef4b52851b7 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -416,8 +416,9 @@ iso_week1_monday(int year) static int iso_to_ymd(const int iso_year, const int iso_week, const int iso_day, int *year, int *month, int *day) { - if (iso_year <= 0) { - return -2; + // Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5) + if (iso_year < MINYEAR || iso_year > MAXYEAR) { + return -4; } if (iso_week <= 0 || iso_week >= 53) { int out_of_range = 1; @@ -765,7 +766,7 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month, * -2: Inconsistent date separator usage * -3: Failed to parse ISO week. * -4: Failed to parse ISO day. - * -5, -6: Failure in iso_to_ymd + * -5, -6, -7: Failure in iso_to_ymd */ const char *p = dtstr; p = parse_digits(p, year, 4); @@ -3145,15 +3146,13 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw) return NULL; } - // Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5) - if (year < MINYEAR || year > MAXYEAR) { - PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year); - return NULL; - } - int month; int rv = iso_to_ymd(year, week, day, &year, &month, &day); + if (rv == -4) { + PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year); + return NULL; + } if (rv == -2) { PyErr_Format(PyExc_ValueError, "Invalid week: %d", week); From 8053d9482688b46851009e51d30d011bb631691e Mon Sep 17 00:00:00 2001 From: Vladislav Efanov Date: Mon, 8 Apr 2024 12:12:04 +0300 Subject: [PATCH 03/11] Add NEWS entry --- Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst diff --git a/Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst b/Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst new file mode 100644 index 00000000000000..5289a267c8e4c7 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst @@ -0,0 +1,2 @@ +Add checking for input parameter in :func:'iso_to_umd' to fix the assertion +in :func:'days_before_year' due to wrong ISO date string. From 82883c4e488e3b2a4369983884c3cf2c98a60390 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:44:30 +0000 Subject: [PATCH 04/11] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst diff --git a/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst b/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst new file mode 100644 index 00000000000000..c588006034c609 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst @@ -0,0 +1,2 @@ +Add checking for input parameter in :func:'iso_to_umd' to fix the assertion +in :func:'days_before_year' due to wrong ISO date string. From c444cff06b8f4356ef44b83b412151656b1a814d Mon Sep 17 00:00:00 2001 From: Vladislav Efanov Date: Mon, 8 Apr 2024 12:51:15 +0300 Subject: [PATCH 05/11] Remove wrong named NEWS --- Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst diff --git a/Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst b/Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst deleted file mode 100644 index 5289a267c8e4c7..00000000000000 --- a/Misc/NEWS.d/next/C API/2024-04-08-11-55-23.gh-117534.alweT3.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add checking for input parameter in :func:'iso_to_umd' to fix the assertion -in :func:'days_before_year' due to wrong ISO date string. From 6c6ac980b12690c5be41a5968638d2ad9c5cea76 Mon Sep 17 00:00:00 2001 From: Vlad4896 <166005126+Vlad4896@users.noreply.github.com> Date: Mon, 8 Apr 2024 19:50:22 +0300 Subject: [PATCH 06/11] Update 2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst Correct syntax. --- .../next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst b/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst index c588006034c609..b405742a82f735 100644 --- a/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst +++ b/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst @@ -1,2 +1,2 @@ -Add checking for input parameter in :func:'iso_to_umd' to fix the assertion -in :func:'days_before_year' due to wrong ISO date string. +Add checking for input parameter in :func:`iso_to_umd` to fix the assertion +in :func:`days_before_year` due to wrong ISO date string. From d9910fbca1e63f381defb89a559d9ee3143f4bb2 Mon Sep 17 00:00:00 2001 From: Vladislav Efanov Date: Mon, 8 Apr 2024 20:08:08 +0300 Subject: [PATCH 07/11] Rework test cases --- Lib/test/datetimetester.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 43b5e6fb43b888..570110893629cf 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -1927,8 +1927,11 @@ def test_fromisoformat_fails(self): '2009-02-29', # Invalid leap day '2019-W53-1', # No week 53 in 2019 '2020-W54-1', # No week 54 + '0000-W25-1', # Invalid year + '10000-W25-1', # Invalid year + '2020-W25-0', # Invalid day-of-week + '2020-W25-8', # Invalid day-of-week '2009\ud80002\ud80028', # Separators are surrogate codepoints - '0000W25', # Invalid year ] for bad_str in bad_strs: From a3e20c3f99fb8dad7df413f9c2e941da62ee735e Mon Sep 17 00:00:00 2001 From: Vlad4896 <166005126+Vlad4896@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:21:13 +0300 Subject: [PATCH 08/11] Update 2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst Update wordings. --- .../next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst b/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst index b405742a82f735..afcdef1c1e44ce 100644 --- a/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst +++ b/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst @@ -1,2 +1,2 @@ -Add checking for input parameter in :func:`iso_to_umd` to fix the assertion -in :func:`days_before_year` due to wrong ISO date string. +Improve validation logic in the C implementation of :meth:`datetime.fromisoformat` +to better handle invalid years. Patch by Vlad Efanov. From 8a32bc723a863b278eabf2ab3e3dd86a82045bb2 Mon Sep 17 00:00:00 2001 From: Vlad4896 <166005126+Vlad4896@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:25:58 +0300 Subject: [PATCH 09/11] Update ACKS --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index fe014a364dd82d..bec3a7bd5ef416 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -2094,5 +2094,6 @@ Jelle Zijlstra Gennadiy Zlobin Doug Zongker Peter Åstrand +Vlad Efanov (Entries should be added in rough alphabetical order by last names) From 6bc747f1d609a6bc65ed6ec923157862b073410e Mon Sep 17 00:00:00 2001 From: Vlad4896 <166005126+Vlad4896@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:35:36 +0300 Subject: [PATCH 10/11] Update ACKS --- Misc/ACKS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/ACKS b/Misc/ACKS index bec3a7bd5ef416..a108ec37d4425e 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -496,6 +496,7 @@ David Edelsohn John Edmonds Benjamin Edwards Grant Edwards +Vlad Efanov Zvi Effron John Ehresman Tal Einat @@ -2094,6 +2095,5 @@ Jelle Zijlstra Gennadiy Zlobin Doug Zongker Peter Åstrand -Vlad Efanov (Entries should be added in rough alphabetical order by last names) From 348743ea9b973a9fd71aea2db050293a9b1be1a5 Mon Sep 17 00:00:00 2001 From: Vladislav Efanov Date: Mon, 8 Apr 2024 20:52:29 +0300 Subject: [PATCH 11/11] remove trailing space --- .../next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst b/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst index afcdef1c1e44ce..4b7dda610fc2b2 100644 --- a/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst +++ b/Misc/NEWS.d/next/C API/2024-04-08-09-44-29.gh-issue-117534.54ZE_n.rst @@ -1,2 +1,2 @@ -Improve validation logic in the C implementation of :meth:`datetime.fromisoformat` -to better handle invalid years. Patch by Vlad Efanov. +Improve validation logic in the C implementation of :meth:`datetime.fromisoformat` +to better handle invalid years. Patch by Vlad Efanov.