3ef2ca
To: vim_dev@googlegroups.com
3ef2ca
Subject: Patch 7.4.593
3ef2ca
Fcc: outbox
3ef2ca
From: Bram Moolenaar <Bram@moolenaar.net>
3ef2ca
Mime-Version: 1.0
3ef2ca
Content-Type: text/plain; charset=UTF-8
3ef2ca
Content-Transfer-Encoding: 8bit
3ef2ca
------------
3ef2ca
3ef2ca
Patch 7.4.593
3ef2ca
Problem:    Crash when searching for "x\{0,90000}". (Dominique Pelle)
3ef2ca
Solution:   Bail out from the NFA engine when the max limit is much higher
3ef2ca
	    than the min limit.
3ef2ca
Files:	    src/regexp_nfa.c, src/regexp.c, src/vim.h
3ef2ca
3ef2ca
3ef2ca
*** ../vim-7.4.592/src/regexp_nfa.c	2015-01-18 16:46:28.983828439 +0100
3ef2ca
--- src/regexp_nfa.c	2015-01-27 12:47:42.515592198 +0100
3ef2ca
***************
3ef2ca
*** 244,249 ****
3ef2ca
--- 244,252 ----
3ef2ca
  static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
3ef2ca
  static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
3ef2ca
  
3ef2ca
+ /* re_flags passed to nfa_regcomp() */
3ef2ca
+ static int nfa_re_flags;
3ef2ca
+ 
3ef2ca
  /* NFA regexp \ze operator encountered. */
3ef2ca
  static int nfa_has_zend;
3ef2ca
  
3ef2ca
***************
3ef2ca
*** 2011,2020 ****
3ef2ca
  	     *  <atom>*  */
3ef2ca
  	    if (minval == 0 && maxval == MAX_LIMIT)
3ef2ca
  	    {
3ef2ca
! 		if (greedy)
3ef2ca
  		    /* \{}, \{0,} */
3ef2ca
  		    EMIT(NFA_STAR);
3ef2ca
! 		else
3ef2ca
  		    /* \{-}, \{-0,} */
3ef2ca
  		    EMIT(NFA_STAR_NONGREEDY);
3ef2ca
  		break;
3ef2ca
--- 2014,2023 ----
3ef2ca
  	     *  <atom>*  */
3ef2ca
  	    if (minval == 0 && maxval == MAX_LIMIT)
3ef2ca
  	    {
3ef2ca
! 		if (greedy)		/* { { (match the braces) */
3ef2ca
  		    /* \{}, \{0,} */
3ef2ca
  		    EMIT(NFA_STAR);
3ef2ca
! 		else			/* { { (match the braces) */
3ef2ca
  		    /* \{-}, \{-0,} */
3ef2ca
  		    EMIT(NFA_STAR_NONGREEDY);
3ef2ca
  		break;
3ef2ca
***************
3ef2ca
*** 2030,2035 ****
3ef2ca
--- 2033,2044 ----
3ef2ca
  		return OK;
3ef2ca
  	    }
3ef2ca
  
3ef2ca
+ 	    /* The engine is very inefficient (uses too many states) when the
3ef2ca
+ 	     * maximum is much larger than the minimum.  Bail out if we can
3ef2ca
+ 	     * use the other engine. */
3ef2ca
+ 	    if ((nfa_re_flags & RE_AUTO) && maxval > minval + 200)
3ef2ca
+ 		return FAIL;
3ef2ca
+ 
3ef2ca
  	    /* Ignore previous call to nfa_regatom() */
3ef2ca
  	    post_ptr = post_start + my_post_start;
3ef2ca
  	    /* Save parse state after the repeated atom and the \{} */
3ef2ca
***************
3ef2ca
*** 7046,7051 ****
3ef2ca
--- 7055,7061 ----
3ef2ca
  	return NULL;
3ef2ca
  
3ef2ca
      nfa_regengine.expr = expr;
3ef2ca
+     nfa_re_flags = re_flags;
3ef2ca
  
3ef2ca
      init_class_tab();
3ef2ca
  
3ef2ca
*** ../vim-7.4.592/src/regexp.c	2014-11-20 23:07:00.515474686 +0100
3ef2ca
--- src/regexp.c	2015-01-27 12:49:02.170719494 +0100
3ef2ca
***************
3ef2ca
*** 8081,8087 ****
3ef2ca
       * First try the NFA engine, unless backtracking was requested.
3ef2ca
       */
3ef2ca
      if (regexp_engine != BACKTRACKING_ENGINE)
3ef2ca
!         prog = nfa_regengine.regcomp(expr, re_flags);
3ef2ca
      else
3ef2ca
  	prog = bt_regengine.regcomp(expr, re_flags);
3ef2ca
  
3ef2ca
--- 8081,8088 ----
3ef2ca
       * First try the NFA engine, unless backtracking was requested.
3ef2ca
       */
3ef2ca
      if (regexp_engine != BACKTRACKING_ENGINE)
3ef2ca
!         prog = nfa_regengine.regcomp(expr,
3ef2ca
! 		re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0));
3ef2ca
      else
3ef2ca
  	prog = bt_regengine.regcomp(expr, re_flags);
3ef2ca
  
3ef2ca
***************
3ef2ca
*** 8105,8120 ****
3ef2ca
  #endif
3ef2ca
  	/*
3ef2ca
  	 * If the NFA engine failed, try the backtracking engine.
3ef2ca
! 	 * Disabled for now, both engines fail on the same patterns.
3ef2ca
! 	 * Re-enable when regcomp() fails when the pattern would work better
3ef2ca
! 	 * with the other engine.
3ef2ca
! 	 *
3ef2ca
  	if (regexp_engine == AUTOMATIC_ENGINE)
3ef2ca
  	{
3ef2ca
  	    prog = bt_regengine.regcomp(expr, re_flags);
3ef2ca
- 	    regexp_engine == BACKTRACKING_ENGINE;
3ef2ca
  	}
3ef2ca
- 	 */
3ef2ca
      }
3ef2ca
  
3ef2ca
      if (prog != NULL)
3ef2ca
--- 8106,8119 ----
3ef2ca
  #endif
3ef2ca
  	/*
3ef2ca
  	 * If the NFA engine failed, try the backtracking engine.
3ef2ca
! 	 * The NFA engine also fails for patterns that it can't handle well
3ef2ca
! 	 * but are still valid patterns, thus a retry should work.
3ef2ca
! 	 */
3ef2ca
  	if (regexp_engine == AUTOMATIC_ENGINE)
3ef2ca
  	{
3ef2ca
+ 	    regexp_engine = BACKTRACKING_ENGINE;
3ef2ca
  	    prog = bt_regengine.regcomp(expr, re_flags);
3ef2ca
  	}
3ef2ca
      }
3ef2ca
  
3ef2ca
      if (prog != NULL)
3ef2ca
*** ../vim-7.4.592/src/vim.h	2014-12-08 04:16:26.269702835 +0100
3ef2ca
--- src/vim.h	2015-01-27 12:41:57.483371986 +0100
3ef2ca
***************
3ef2ca
*** 1020,1025 ****
3ef2ca
--- 1020,1026 ----
3ef2ca
  #define RE_MAGIC	1	/* 'magic' option */
3ef2ca
  #define RE_STRING	2	/* match in string instead of buffer text */
3ef2ca
  #define RE_STRICT	4	/* don't allow [abc] without ] */
3ef2ca
+ #define RE_AUTO		8	/* automatic engine selection */
3ef2ca
  
3ef2ca
  #ifdef FEAT_SYN_HL
3ef2ca
  /* values for reg_do_extmatch */
3ef2ca
*** ../vim-7.4.592/src/version.c	2015-01-27 11:26:11.041183653 +0100
3ef2ca
--- src/version.c	2015-01-27 12:52:44.720281369 +0100
3ef2ca
***************
3ef2ca
*** 743,744 ****
3ef2ca
--- 743,746 ----
3ef2ca
  {   /* Add new patch number below this line */
3ef2ca
+ /**/
3ef2ca
+     593,
3ef2ca
  /**/
3ef2ca
3ef2ca
-- 
3ef2ca
hundred-and-one symptoms of being an internet addict:
3ef2ca
121. You ask for e-mail adresses instead of telephone numbers.
3ef2ca
3ef2ca
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
3ef2ca
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
3ef2ca
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
3ef2ca
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///