Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -3263,6 +3263,14 @@ def test_sre_template_invalid_group_index(self):
_sre.template("", ["", (), ""])
self.assertIn("an integer is required", str(cm.exception))

@cpython_only
def test_sre_IN_invalid_skip(self):
# gh-160814: the validator must reject the IN opcode with skip < 2.
import _sre
for skip in (0, 1):
with self.assertRaises(RuntimeError):
_sre.compile("x", 0, [13, skip, 1], 0, {}, ())


class ExternalTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in :meth:`!_sre.compile` with an ``IN`` opcode whose ``skip`` value
is smaller than 2.
2 changes: 2 additions & 0 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,8 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
case SRE_OP_IN_UNI_IGNORE:
case SRE_OP_IN_LOC_IGNORE:
GET_SKIP;
if (skip < 2)
FAIL;
/* Stop 1 before the end; we check the FAILURE below */
if (_validate_charset(code, code+skip-2))
FAIL;
Expand Down
Loading