From 530247b7734659e6f97ba8235950e2084b76074c Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Tue, 10 Oct 2023 13:15:12 +0300 Subject: [PATCH] gh-110590: Fix a bug where _sre.compile would overwrite exceptions (GH-110591) TypeError would be overwritten by OverflowError if 'code' param contained non-ints. (cherry picked from commit 344d3a222a7864f8157773749bdd77d1c9dfc1e6) Co-authored-by: Nikita Sobolev --- Lib/test/test_re.py | 3 +++ .../Library/2023-10-10-10-46-55.gh-issue-110590.fatz-h.rst | 3 +++ Modules/_sre/sre.c | 3 +++ 3 files changed, 9 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-10-10-10-46-55.gh-issue-110590.fatz-h.rst diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index f4d64dc9fcf2ae..b41bcd49385453 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -2725,6 +2725,9 @@ def test_dealloc(self): _sre.compile("abc", 0, [long_overflow], 0, {}, ()) with self.assertRaises(TypeError): _sre.compile({}, 0, [], 0, [], []) + # gh-110590: `TypeError` was overwritten with `OverflowError`: + with self.assertRaises(TypeError): + _sre.compile('', 0, ['abc'], 0, {}, ()) @cpython_only def test_repeat_minmax_overflow_maxrepeat(self): diff --git a/Misc/NEWS.d/next/Library/2023-10-10-10-46-55.gh-issue-110590.fatz-h.rst b/Misc/NEWS.d/next/Library/2023-10-10-10-46-55.gh-issue-110590.fatz-h.rst new file mode 100644 index 00000000000000..20dc3fff205994 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-10-10-46-55.gh-issue-110590.fatz-h.rst @@ -0,0 +1,3 @@ +Fix a bug in :meth:`!_sre.compile` where :exc:`TypeError` +would be overwritten by :exc:`OverflowError` when +the *code* argument was a list of non-ints. diff --git a/Modules/_sre/sre.c b/Modules/_sre/sre.c index 448e761c988ca8..81629f0007e6a1 100644 --- a/Modules/_sre/sre.c +++ b/Modules/_sre/sre.c @@ -1437,6 +1437,9 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags, for (i = 0; i < n; i++) { PyObject *o = PyList_GET_ITEM(code, i); unsigned long value = PyLong_AsUnsignedLong(o); + if (value == (unsigned long)-1 && PyErr_Occurred()) { + break; + } self->code[i] = (SRE_CODE) value; if ((unsigned long) self->code[i] != value) { PyErr_SetString(PyExc_OverflowError,