mirror of
https://github.com/python/cpython.git
synced 2025-08-27 04:05:34 +00:00
[3.11] gh-109747: Improve errors for unsupported look-behind patterns (GH-109859) (GH-110860)
Now re.error is raised instead of OverflowError or RuntimeError for
too large width of look-behind pattern.
The limit is increased to 2**32-1 (was 2**31-1).
(cherry picked from commit e2b3d831fd
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
035f9e0cc5
commit
1c26f1ce6c
6 changed files with 46 additions and 13 deletions
|
@ -68,6 +68,10 @@ FLAGS = {
|
|||
TYPE_FLAGS = SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE
|
||||
GLOBAL_FLAGS = SRE_FLAG_DEBUG | SRE_FLAG_TEMPLATE
|
||||
|
||||
# Maximal value returned by SubPattern.getwidth().
|
||||
# Must be larger than MAXREPEAT, MAXCODE and sys.maxsize.
|
||||
MAXWIDTH = 1 << 64
|
||||
|
||||
class State:
|
||||
# keeps track of state for parsing
|
||||
def __init__(self):
|
||||
|
@ -178,7 +182,7 @@ class SubPattern:
|
|||
lo = hi = 0
|
||||
for op, av in self.data:
|
||||
if op is BRANCH:
|
||||
i = MAXREPEAT - 1
|
||||
i = MAXWIDTH
|
||||
j = 0
|
||||
for av in av[1]:
|
||||
l, h = av.getwidth()
|
||||
|
@ -197,7 +201,10 @@ class SubPattern:
|
|||
elif op in _REPEATCODES:
|
||||
i, j = av[2].getwidth()
|
||||
lo = lo + i * av[0]
|
||||
hi = hi + j * av[1]
|
||||
if av[1] == MAXREPEAT and j:
|
||||
hi = MAXWIDTH
|
||||
else:
|
||||
hi = hi + j * av[1]
|
||||
elif op in _UNITCODES:
|
||||
lo = lo + 1
|
||||
hi = hi + 1
|
||||
|
@ -217,7 +224,7 @@ class SubPattern:
|
|||
hi = hi + j
|
||||
elif op is SUCCESS:
|
||||
break
|
||||
self.width = min(lo, MAXREPEAT - 1), min(hi, MAXREPEAT)
|
||||
self.width = min(lo, MAXWIDTH), min(hi, MAXWIDTH)
|
||||
return self.width
|
||||
|
||||
class Tokenizer:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue