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