Skip to content

Commit fa9c3ee

Browse files
authored
gh-140797: Forbid capturing groups in re.Scanner lexicon patterns (GH-140944)
1 parent a84181c commit fa9c3ee

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/re/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,12 @@ def __init__(self, lexicon, flags=0):
397397
s = _parser.State()
398398
s.flags = flags
399399
for phrase, action in lexicon:
400+
sub_pattern = _parser.parse(phrase, flags)
401+
if sub_pattern.state.groups != 1:
402+
raise ValueError("Cannot use capturing groups in re.Scanner")
400403
gid = s.opengroup()
401404
p.append(_parser.SubPattern(s, [
402-
(SUBPATTERN, (gid, 0, 0, _parser.parse(phrase, flags))),
405+
(SUBPATTERN, (gid, 0, 0, sub_pattern)),
403406
]))
404407
s.closegroup(gid, p[-1])
405408
p = _parser.SubPattern(s, [(BRANCH, (None, p))])

Lib/test/test_re.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,24 @@ def s_int(scanner, token): return int(token)
16391639
(['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
16401640
'op+', 'bar'], ''))
16411641

1642+
def test_bug_gh140797(self):
1643+
# gh140797: Capturing groups are not allowed in re.Scanner
1644+
1645+
msg = r"Cannot use capturing groups in re\.Scanner"
1646+
# Capturing group throws an error
1647+
with self.assertRaisesRegex(ValueError, msg):
1648+
Scanner([("(a)b", None)])
1649+
1650+
# Named Group
1651+
with self.assertRaisesRegex(ValueError, msg):
1652+
Scanner([("(?P<name>a)", None)])
1653+
1654+
# Non-capturing groups should pass normally
1655+
s = Scanner([("(?:a)b", lambda scanner, token: token)])
1656+
result, rem = s.scan("ab")
1657+
self.assertEqual(result,['ab'])
1658+
self.assertEqual(rem,'')
1659+
16421660
def test_bug_448951(self):
16431661
# bug 448951 (similar to 429357, but with single char match)
16441662
# (Also test greedy matches.)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The undocumented :class:`!re.Scanner` class now forbids regular expressions containing capturing groups in its lexicon patterns. Patterns using capturing groups could
2+
previously lead to crashes with segmentation fault. Use non-capturing groups (?:...) instead.

0 commit comments

Comments
 (0)