Skip to content

Commit 76ff686

Browse files
[3.9] gh-91700: Validate the group number in conditional expression in RE (GH-91702) (GH-91831) (GH-91836)
In expression (?(group)...) an appropriate re.error is now raised if the group number refers to not defined group. Previously it raised RuntimeError: invalid SRE code. (cherry picked from commit 48ec61a) (cherry picked from commit 080781c) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 97d14e1 commit 76ff686

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Lib/sre_parse.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def __init__(self):
7878
self.groupdict = {}
7979
self.groupwidths = [None] # group 0
8080
self.lookbehindgroups = None
81+
self.grouprefpos = {}
8182
@property
8283
def groups(self):
8384
return len(self.groupwidths)
@@ -786,6 +787,10 @@ def _parse(source, state, verbose, nested, first=False):
786787
if condgroup >= MAXGROUPS:
787788
msg = "invalid group reference %d" % condgroup
788789
raise source.error(msg, len(condname) + 1)
790+
if condgroup not in state.grouprefpos:
791+
state.grouprefpos[condgroup] = (
792+
source.tell() - len(condname) - 1
793+
)
789794
state.checklookbehindgroup(condgroup, source)
790795
item_yes = _parse(source, state, verbose, nested + 1)
791796
if source.match("|"):
@@ -963,6 +968,11 @@ def parse(str, flags=0, state=None):
963968
assert source.next == ")"
964969
raise source.error("unbalanced parenthesis")
965970

971+
for g in p.state.grouprefpos:
972+
if g >= p.state.groups:
973+
msg = "invalid group reference %d" % g
974+
raise error(msg, str, p.state.grouprefpos[g])
975+
966976
if flags & SRE_FLAG_DEBUG:
967977
p.dump()
968978

Lib/test/test_re.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ def test_re_groupref_exists_errors(self):
574574
self.checkPatternError(r'()(?(1)a|b|c)',
575575
'conditional backref with more than '
576576
'two branches', 10)
577+
self.checkPatternError(r'()(?(2)a)',
578+
"invalid group reference 2", 5)
577579

578580
def test_re_groupref_overflow(self):
579581
from sre_constants import MAXGROUPS
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Compilation of regular expression containing a conditional expression
2+
``(?(group)...)`` now raises an appropriate :exc:`re.error` if the group
3+
number refers to not defined group. Previously an internal RuntimeError was
4+
raised.

0 commit comments

Comments
 (0)