diff options
Diffstat (limited to 'Lib/sre_parse.py')
-rw-r--r-- | Lib/sre_parse.py | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index b56d437..b9a1852 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -72,6 +72,8 @@ class Pattern: def opengroup(self, name=None): gid = self.groups self.groups = gid + 1 + if self.groups > MAXGROUPS: + raise error("groups number is too large") if name is not None: ogid = self.groupdict.get(name, None) if ogid is not None: @@ -295,7 +297,11 @@ def _class_escape(source, escape): elif c in OCTDIGITS: # octal escape (up to three digits) escape += source.getwhile(2, OCTDIGITS) - return LITERAL, int(escape[1:], 8) & 0xff + c = int(escape[1:], 8) + if c > 0o377: + raise error('octal escape value %r outside of ' + 'range 0-0o377' % escape) + return LITERAL, c elif c in DIGITS: raise ValueError if len(escape) == 2: @@ -337,7 +343,7 @@ def _escape(source, escape, state): elif c == "0": # octal escape escape += source.getwhile(2, OCTDIGITS) - return LITERAL, int(escape[1:], 8) & 0xff + return LITERAL, int(escape[1:], 8) elif c in DIGITS: # octal escape *or* decimal group reference (sigh) if source.next in DIGITS: @@ -346,7 +352,11 @@ def _escape(source, escape, state): source.next in OCTDIGITS): # got three octal digits; this is an octal escape escape = escape + source.get() - return LITERAL, int(escape[1:], 8) & 0xff + c = int(escape[1:], 8) + if c > 0o377: + raise error('octal escape value %r outside of ' + 'range 0-0o377' % escape) + return LITERAL, c # not an octal escape, so this is a group reference group = int(escape[1:]) if group < state.groups: @@ -687,8 +697,14 @@ def _parse(source, state): else: try: condgroup = int(condname) + if condgroup < 0: + raise ValueError except ValueError: raise error("bad character in group name") + if not condgroup: + raise error("bad group number") + if condgroup >= MAXGROUPS: + raise error("the group number is too large") else: # flags if not source.next in FLAGS: @@ -814,6 +830,8 @@ def parse_template(source, pattern): index = int(name) if index < 0: raise error("negative group number") + if index >= MAXGROUPS: + raise error("the group number is too large") except ValueError: if not name.isidentifier(): raise error("bad character in group name") @@ -837,7 +855,11 @@ def parse_template(source, pattern): s.next in OCTDIGITS): this += sget() isoctal = True - lappend(chr(int(this[1:], 8) & 0xff)) + c = int(this[1:], 8) + if c > 0o377: + raise error('octal escape value %r outside of ' + 'range 0-0o377' % this) + lappend(chr(c)) if not isoctal: addgroup(int(this[1:])) else: |