[Python-checkins] CVS: python/dist/src/Tools/idle PyParse.py,1.4,1.5

Guido van Rossum guido@cnri.reston.va.us
Fri, 3 Mar 2000 09:51:13 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Tools/idle
In directory eric:/projects/python/develop/guido/src/Tools/idle

Modified Files:
	PyParse.py 
Log Message:
Patch by Tim Peters:

Changes the one regexp in PyParse capable of making the re module blow the C
stack when passed unreasonable <0.9 wink> program text.  Jeremy Hylton
provoked this with a program of the form:

x = (1,
     2,
... # 9997 lines deleted here
     10000,
)

Programs "like this" will no longer (no matter how many lines they contain)
trigger re death.  OTOH, you can now make another class of unreasonable
program that will take much longer to parse.


Index: PyParse.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Tools/idle/PyParse.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** PyParse.py	1999/06/07 14:28:14	1.4
--- PyParse.py	2000/03/03 14:51:11	1.5
***************
*** 84,96 ****
  """, re.VERBOSE).match
  
! # Chew up non-special chars as quickly as possible, but retaining
! # enough info to determine the last non-ws char seen; if match is
! # successful, and m.group(1) isn't None, m.end(1) less 1 is the
! # index of the last non-ws char matched.
  
  _chew_ordinaryre = re.compile(r"""
!     (?: \s+
!     |   ( [^\s[\](){}#'"\\]+ )
!     )+
  """, re.VERBOSE).match
  
--- 84,94 ----
  """, re.VERBOSE).match
  
! # Chew up non-special chars as quickly as possible.  If match is
! # successful, m.end() less 1 is the index of the last boring char
! # matched.  If match is unsuccessful, the string starts with an
! # interesting char.
  
  _chew_ordinaryre = re.compile(r"""
!     [^[\](){}#'"\\]+
  """, re.VERBOSE).match
  
***************
*** 387,394 ****
              m = _chew_ordinaryre(str, p, q)
              if m:
!                 i = m.end(1) - 1    # last non-ws (if any)
                  if i >= 0:
                      lastch = str[i]
-                 p = m.end()
                  if p >= q:
                      break
--- 385,396 ----
              m = _chew_ordinaryre(str, p, q)
              if m:
!                 # we skipped at least one boring char
!                 p = m.end()
!                 # back up over totally boring whitespace
!                 i = p-1    # index of last boring char
!                 while i >= 0 and str[i] in " \t\n":
!                     i = i-1
                  if i >= 0:
                      lastch = str[i]
                  if p >= q:
                      break