mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-106524: Fix a crash in _sre.template() (GH-106525)
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.
This commit is contained in:
parent
1c9e493462
commit
2ef1dc37f0
3 changed files with 13 additions and 0 deletions
|
@ -2418,6 +2418,16 @@ class ReTests(unittest.TestCase):
|
||||||
p.terminate()
|
p.terminate()
|
||||||
p.join()
|
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):
|
def get_debug_out(pat):
|
||||||
with captured_stdout() as out:
|
with captured_stdout() as out:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix crash in :func:`!_sre.template` with templates containing invalid group indices.
|
|
@ -1544,10 +1544,12 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
|
||||||
for (Py_ssize_t i = 0; i < n; i++) {
|
for (Py_ssize_t i = 0; i < n; i++) {
|
||||||
Py_ssize_t index = PyLong_AsSsize_t(PyList_GET_ITEM(template, 2*i+1));
|
Py_ssize_t index = PyLong_AsSsize_t(PyList_GET_ITEM(template, 2*i+1));
|
||||||
if (index == -1 && PyErr_Occurred()) {
|
if (index == -1 && PyErr_Occurred()) {
|
||||||
|
Py_SET_SIZE(self, i);
|
||||||
Py_DECREF(self);
|
Py_DECREF(self);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
|
Py_SET_SIZE(self, i);
|
||||||
goto bad_template;
|
goto bad_template;
|
||||||
}
|
}
|
||||||
self->items[i].index = index;
|
self->items[i].index = index;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue