From 506d2c67cbe2bf38c6700848d25c06903574f115 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 9 Jun 2023 22:43:00 +0200 Subject: [PATCH 1/2] gh-105375: Harden _datetime initialisation Improve error handling so init bails on the first exception. --- ...-06-09-22-45-26.gh-issue-105375.9rp6tG.rst | 2 ++ Modules/_datetimemodule.c | 35 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst diff --git a/Misc/NEWS.d/next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst b/Misc/NEWS.d/next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst new file mode 100644 index 00000000000000..900c98596c6915 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst @@ -0,0 +1,2 @@ +Fix bugs in :mod:`_datetime` where exceptions could be overwritten in case +of module initialisation failure. diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 57f817dad8f842..db2d339fc36db4 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -6862,24 +6862,49 @@ _datetime_exec(PyObject *module) assert(DI100Y == days_before_year(100+1)); us_per_ms = PyLong_FromLong(1000); + if (us_per_ms == NULL) { + goto error; + } us_per_second = PyLong_FromLong(1000000); + if (us_per_second == NULL) { + goto error; + } us_per_minute = PyLong_FromLong(60000000); + if (us_per_minute == NULL) { + goto error; + } seconds_per_day = PyLong_FromLong(24 * 3600); - if (us_per_ms == NULL || us_per_second == NULL || - us_per_minute == NULL || seconds_per_day == NULL) { - return -1; + if (seconds_per_day == NULL) { + goto error; } /* The rest are too big for 32-bit ints, but even * us_per_week fits in 40 bits, so doubles should be exact. */ us_per_hour = PyLong_FromDouble(3600000000.0); + if (us_per_hour == NULL) { + goto error; + } us_per_day = PyLong_FromDouble(86400000000.0); + if (us_per_day == NULL) { + goto error; + } us_per_week = PyLong_FromDouble(604800000000.0); - if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) { - return -1; + if (us_per_week == NULL) { + goto error; } + return 0; + +error: + Py_XDECREF(us_per_ms); + Py_XDECREF(us_per_second); + Py_XDECREF(us_per_minute); + Py_XDECREF(us_per_hour); + Py_XDECREF(us_per_day); + Py_XDECREF(us_per_week); + Py_XDECREF(seconds_per_day); + return -1; } static struct PyModuleDef datetimemodule = { From a51263065d8e34422bbc11d5f0518417bdadeaf8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 9 Jun 2023 23:03:34 +0200 Subject: [PATCH 2/2] Sphinx formatting --- .../next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst b/Misc/NEWS.d/next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst index 900c98596c6915..352d7b83a71632 100644 --- a/Misc/NEWS.d/next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst +++ b/Misc/NEWS.d/next/Library/2023-06-09-22-45-26.gh-issue-105375.9rp6tG.rst @@ -1,2 +1,2 @@ -Fix bugs in :mod:`_datetime` where exceptions could be overwritten in case +Fix bugs in :mod:`!_datetime` where exceptions could be overwritten in case of module initialisation failure.