]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-106524: Fix a crash in _sre.template() (GH-106525) (GH-106544)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 8 Jul 2023 08:12:33 +0000 (01:12 -0700)
committerGitHub <noreply@github.com>
Sat, 8 Jul 2023 08:12:33 +0000 (08:12 +0000)
Some items remained uninitialized if _sre.template() was called with invalid
indices. Then attempt to clear them in the destructor led to dereferencing
of uninitialized pointer.
(cherry picked from commit 2ef1dc37f02b08536b677dd23ec51541a60effd7)

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Lib/test/test_re.py
Misc/NEWS.d/next/Library/2023-07-07-17-44-03.gh-issue-106524.XkBV8h.rst [new file with mode: 0644]
Modules/_sre/sre.c

index 11628a236ade9a3b72989420dc7d7945df99e62b..a05a1c99def0bc02d3781e60f04e335ed0a20de2 100644 (file)
@@ -2441,6 +2441,16 @@ class ReTests(unittest.TestCase):
                 p.terminate()
                 p.join()
 
+    def test_sre_template_invalid_group_index(self):
+        # see gh-106524
+        import _sre
+        with self.assertRaises(TypeError) as cm:
+            _sre.template("", ["", -1, ""])
+        self.assertIn("invalid template", str(cm.exception))
+        with self.assertRaises(TypeError) as cm:
+            _sre.template("", ["", (), ""])
+        self.assertIn("an integer is required", str(cm.exception))
+
 
 def get_debug_out(pat):
     with captured_stdout() as out:
diff --git a/Misc/NEWS.d/next/Library/2023-07-07-17-44-03.gh-issue-106524.XkBV8h.rst b/Misc/NEWS.d/next/Library/2023-07-07-17-44-03.gh-issue-106524.XkBV8h.rst
new file mode 100644 (file)
index 0000000..f3fd070
--- /dev/null
@@ -0,0 +1 @@
+Fix crash in :func:`!_sre.template` with templates containing invalid group indices.
index f8a1a05a3188892302af9ee39e70428d957c220d..2f1c7324a0fa464e5bbb79ea6f8a6aa1a4d11f3d 100644 (file)
@@ -1549,10 +1549,12 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
     for (Py_ssize_t i = 0; i < n; i++) {
         Py_ssize_t index = PyLong_AsSsize_t(PyList_GET_ITEM(template, 2*i+1));
         if (index == -1 && PyErr_Occurred()) {
+            Py_SET_SIZE(self, i);
             Py_DECREF(self);
             return NULL;
         }
         if (index < 0) {
+            Py_SET_SIZE(self, i);
             goto bad_template;
         }
         self->items[i].index = index;