diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-04-22 18:09:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-22 18:09:30 (GMT) |
commit | 080781cd49b13da4a73db87b6f5e0c7aeec83e92 (patch) | |
tree | fda64bfab4afd96dd7a12360f857eaefc3303877 /Lib/sre_parse.py | |
parent | 9c18d783c38fca57a63b61aa778d8a8d18945d95 (diff) | |
download | cpython-080781cd49b13da4a73db87b6f5e0c7aeec83e92.zip cpython-080781cd49b13da4a73db87b6f5e0c7aeec83e92.tar.gz cpython-080781cd49b13da4a73db87b6f5e0c7aeec83e92.tar.bz2 |
[3.10] gh-91700: Validate the group number in conditional expression in RE (GH-91702) (GH-91831)
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 48ec61a89a959071206549819448405c2cea61b0)
Diffstat (limited to 'Lib/sre_parse.py')
-rw-r--r-- | Lib/sre_parse.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index d3ff196..20a6025 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -78,6 +78,7 @@ class State: self.groupdict = {} self.groupwidths = [None] # group 0 self.lookbehindgroups = None + self.grouprefpos = {} @property def groups(self): return len(self.groupwidths) @@ -786,6 +787,10 @@ def _parse(source, state, verbose, nested, first=False): if condgroup >= MAXGROUPS: msg = "invalid group reference %d" % condgroup raise source.error(msg, len(condname) + 1) + if condgroup not in state.grouprefpos: + state.grouprefpos[condgroup] = ( + source.tell() - len(condname) - 1 + ) state.checklookbehindgroup(condgroup, source) item_yes = _parse(source, state, verbose, nested + 1) if source.match("|"): @@ -963,6 +968,11 @@ def parse(str, flags=0, state=None): assert source.next == ")" raise source.error("unbalanced parenthesis") + for g in p.state.grouprefpos: + if g >= p.state.groups: + msg = "invalid group reference %d" % g + raise error(msg, str, p.state.grouprefpos[g]) + if flags & SRE_FLAG_DEBUG: p.dump() |