]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-110590: Fix a bug where _sre.compile would overwrite exceptions (GH-110591...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 10 Oct 2023 10:55:21 +0000 (12:55 +0200)
committerGitHub <noreply@github.com>
Tue, 10 Oct 2023 10:55:21 +0000 (10:55 +0000)
TypeError would be overwritten by OverflowError
if 'code' param contained non-ints.
(cherry picked from commit 344d3a222a7864f8157773749bdd77d1c9dfc1e6)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/test/test_re.py
Misc/NEWS.d/next/Library/2023-10-10-10-46-55.gh-issue-110590.fatz-h.rst [new file with mode: 0644]
Modules/_sre/sre.c

index 5a5de523eba0526a78c7feea0ef5c5690f69c1b4..382ef0b33cc05f1eaf630b52a367e9b63e5008c3 100644 (file)
@@ -2694,6 +2694,9 @@ class ImplementationTest(unittest.TestCase):
             _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 (file)
index 0000000..20dc3ff
--- /dev/null
@@ -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.
index 2f1c7324a0fa464e5bbb79ea6f8a6aa1a4d11f3d..ace2e9bd7cc2f6bac4e5c60f8ea9bccaff125702 100644 (file)
@@ -1462,6 +1462,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,