File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -681,6 +681,15 @@ def test_stack_overflow(self):
681681 self .assertEqual (re .match ('(x)*y' , 50000 * 'x' + 'y' ).group (1 ), 'x' )
682682 self .assertEqual (re .match ('(x)*?y' , 50000 * 'x' + 'y' ).group (1 ), 'x' )
683683
684+ def test_unlimited_zero_width_repeat (self ):
685+ # Issue #9669
686+ self .assertIsNone (re .match (r'(?:a?)*y' , 'z' ))
687+ self .assertIsNone (re .match (r'(?:a?)+y' , 'z' ))
688+ self .assertIsNone (re .match (r'(?:a?){2,}y' , 'z' ))
689+ self .assertIsNone (re .match (r'(?:a?)*?y' , 'z' ))
690+ self .assertIsNone (re .match (r'(?:a?)+?y' , 'z' ))
691+ self .assertIsNone (re .match (r'(?:a?){2,}?y' , 'z' ))
692+
684693 def test_scanner (self ):
685694 def s_ident (scanner , token ): return token
686695 def s_operator (scanner , token ): return "op%s" % token
Original file line number Diff line number Diff line change @@ -255,6 +255,9 @@ Core and Builtins
255255Library
256256-------
257257
258+ - Issue #9669: Protect re against infinite loops on zero-width matching in
259+ non-greedy repeat. Patch by Matthew Barnett.
260+
258261- Issue #13169: The maximal repetition number in a regular expression has been
259262 increased from 65534 to 2147483647 (on 32-bit platform) or 4294967294 (on
260263 64-bit).
Original file line number Diff line number Diff line change @@ -1272,13 +1272,18 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
12721272
12731273 LASTMARK_RESTORE ();
12741274
1275- if (ctx -> count >= ctx -> u .rep -> pattern [2 ]
1276- && ctx -> u .rep -> pattern [2 ] != SRE_MAXREPEAT )
1275+ if ((ctx -> count >= ctx -> u .rep -> pattern [2 ]
1276+ && ctx -> u .rep -> pattern [2 ] != SRE_MAXREPEAT ) ||
1277+ state -> ptr == ctx -> u .rep -> last_ptr )
12771278 RETURN_FAILURE ;
12781279
12791280 ctx -> u .rep -> count = ctx -> count ;
1281+ /* zero-width match protection */
1282+ DATA_PUSH (& ctx -> u .rep -> last_ptr );
1283+ ctx -> u .rep -> last_ptr = state -> ptr ;
12801284 DO_JUMP (JUMP_MIN_UNTIL_3 ,jump_min_until_3 ,
12811285 ctx -> u .rep -> pattern + 3 );
1286+ DATA_POP (& ctx -> u .rep -> last_ptr );
12821287 if (ret ) {
12831288 RETURN_ON_ERROR (ret );
12841289 RETURN_SUCCESS ;
You can’t perform that action at this time.
0 commit comments