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