Karsten Hopp 4e0c26
To: vim_dev@googlegroups.com
Karsten Hopp 4e0c26
Subject: Patch 7.3.1280
Karsten Hopp 4e0c26
Fcc: outbox
Karsten Hopp 4e0c26
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp 4e0c26
Mime-Version: 1.0
Karsten Hopp 4e0c26
Content-Type: text/plain; charset=UTF-8
Karsten Hopp 4e0c26
Content-Transfer-Encoding: 8bit
Karsten Hopp 4e0c26
------------
Karsten Hopp 4e0c26
Karsten Hopp 4e0c26
Patch 7.3.1280
Karsten Hopp 4e0c26
Problem:    Reading memory already freed since patch 7.3.1247. (Simon
Karsten Hopp 4e0c26
	    Ruderich, Dominique Pelle)
Karsten Hopp 4e0c26
Solution:   Copy submatches before reallocating the state list.
Karsten Hopp 4e0c26
Files:	    src/regexp_nfa.c
Karsten Hopp 4e0c26
Karsten Hopp 4e0c26
Karsten Hopp 4e0c26
*** ../vim-7.3.1279/src/regexp_nfa.c	2013-06-30 13:17:18.000000000 +0200
Karsten Hopp 4e0c26
--- src/regexp_nfa.c	2013-06-30 23:17:46.000000000 +0200
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 3538,3544 ****
Karsten Hopp 4e0c26
  static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Karsten Hopp 4e0c26
  static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Karsten Hopp 4e0c26
  static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Karsten Hopp 4e0c26
! static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int off));
Karsten Hopp 4e0c26
  static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip));
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  /*
Karsten Hopp 4e0c26
--- 3538,3544 ----
Karsten Hopp 4e0c26
  static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Karsten Hopp 4e0c26
  static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Karsten Hopp 4e0c26
  static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Karsten Hopp 4e0c26
! static regsubs_T *addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off));
Karsten Hopp 4e0c26
  static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip));
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  /*
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 3832,3844 ****
Karsten Hopp 4e0c26
      return FALSE;
Karsten Hopp 4e0c26
  }
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
!     static void
Karsten Hopp 4e0c26
! addstate(l, state, subs, pim, off)
Karsten Hopp 4e0c26
!     nfa_list_T		*l;	/* runtime state list */
Karsten Hopp 4e0c26
!     nfa_state_T		*state;	/* state to update */
Karsten Hopp 4e0c26
!     regsubs_T		*subs;	/* pointers to subexpressions */
Karsten Hopp 4e0c26
!     nfa_pim_T		*pim;   /* postponed look-behind match */
Karsten Hopp 4e0c26
!     int			off;	/* byte offset, when -1 go to next line */
Karsten Hopp 4e0c26
  {
Karsten Hopp 4e0c26
      int			subidx;
Karsten Hopp 4e0c26
      nfa_thread_T	*thread;
Karsten Hopp 4e0c26
--- 3832,3849 ----
Karsten Hopp 4e0c26
      return FALSE;
Karsten Hopp 4e0c26
  }
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
! /*
Karsten Hopp 4e0c26
!  * Add "state" and possibly what follows to state list ".".
Karsten Hopp 4e0c26
!  * Returns "subs_arg", possibly copied into temp_subs.
Karsten Hopp 4e0c26
!  */
Karsten Hopp 4e0c26
! 
Karsten Hopp 4e0c26
!     static regsubs_T *
Karsten Hopp 4e0c26
! addstate(l, state, subs_arg, pim, off)
Karsten Hopp 4e0c26
!     nfa_list_T		*l;	    /* runtime state list */
Karsten Hopp 4e0c26
!     nfa_state_T		*state;	    /* state to update */
Karsten Hopp 4e0c26
!     regsubs_T		*subs_arg;  /* pointers to subexpressions */
Karsten Hopp 4e0c26
!     nfa_pim_T		*pim;	    /* postponed look-behind match */
Karsten Hopp 4e0c26
!     int			off;	    /* byte offset, when -1 go to next line */
Karsten Hopp 4e0c26
  {
Karsten Hopp 4e0c26
      int			subidx;
Karsten Hopp 4e0c26
      nfa_thread_T	*thread;
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 3847,3852 ****
Karsten Hopp 4e0c26
--- 3852,3859 ----
Karsten Hopp 4e0c26
      char_u		*save_ptr;
Karsten Hopp 4e0c26
      int			i;
Karsten Hopp 4e0c26
      regsub_T		*sub;
Karsten Hopp 4e0c26
+     regsubs_T		*subs = subs_arg;
Karsten Hopp 4e0c26
+     static regsubs_T	temp_subs;
Karsten Hopp 4e0c26
  #ifdef ENABLE_LOG
Karsten Hopp 4e0c26
      int			did_print = FALSE;
Karsten Hopp 4e0c26
  #endif
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 3941,3947 ****
Karsten Hopp 4e0c26
  		    fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
Karsten Hopp 4e0c26
  			    abs(state->id), l->id, state->c, code);
Karsten Hopp 4e0c26
  #endif
Karsten Hopp 4e0c26
! 		    return;
Karsten Hopp 4e0c26
  		}
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  		/* Do not add the state again when it exists with the same
Karsten Hopp 4e0c26
--- 3948,3954 ----
Karsten Hopp 4e0c26
  		    fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
Karsten Hopp 4e0c26
  			    abs(state->id), l->id, state->c, code);
Karsten Hopp 4e0c26
  #endif
Karsten Hopp 4e0c26
! 		    return subs;
Karsten Hopp 4e0c26
  		}
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  		/* Do not add the state again when it exists with the same
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 3956,3961 ****
Karsten Hopp 4e0c26
--- 3963,3980 ----
Karsten Hopp 4e0c26
  	    {
Karsten Hopp 4e0c26
  		int newlen = l->len * 3 / 2 + 50;
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
+ 		if (subs != &temp_subs)
Karsten Hopp 4e0c26
+ 		{
Karsten Hopp 4e0c26
+ 		    /* "subs" may point into the current array, need to make a
Karsten Hopp 4e0c26
+ 		     * copy before it becomes invalid. */
Karsten Hopp 4e0c26
+ 		    copy_sub(&temp_subs.norm, &subs->norm);
Karsten Hopp 4e0c26
+ #ifdef FEAT_SYN_HL
Karsten Hopp 4e0c26
+ 		    if (nfa_has_zsubexpr)
Karsten Hopp 4e0c26
+ 			copy_sub(&temp_subs.synt, &subs->synt);
Karsten Hopp 4e0c26
+ #endif
Karsten Hopp 4e0c26
+ 		    subs = &temp_subs;
Karsten Hopp 4e0c26
+ 		}
Karsten Hopp 4e0c26
+ 
Karsten Hopp 4e0c26
  		l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
Karsten Hopp 4e0c26
  		l->len = newlen;
Karsten Hopp 4e0c26
  	    }
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 3991,4004 ****
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	case NFA_SPLIT:
Karsten Hopp 4e0c26
  	    /* order matters here */
Karsten Hopp 4e0c26
! 	    addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
! 	    addstate(l, state->out1, subs, pim, off);
Karsten Hopp 4e0c26
  	    break;
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	case NFA_SKIP_CHAR:
Karsten Hopp 4e0c26
  	case NFA_NOPEN:
Karsten Hopp 4e0c26
  	case NFA_NCLOSE:
Karsten Hopp 4e0c26
! 	    addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
  	    break;
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	case NFA_MOPEN:
Karsten Hopp 4e0c26
--- 4010,4023 ----
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	case NFA_SPLIT:
Karsten Hopp 4e0c26
  	    /* order matters here */
Karsten Hopp 4e0c26
! 	    subs = addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
! 	    subs = addstate(l, state->out1, subs, pim, off);
Karsten Hopp 4e0c26
  	    break;
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	case NFA_SKIP_CHAR:
Karsten Hopp 4e0c26
  	case NFA_NOPEN:
Karsten Hopp 4e0c26
  	case NFA_NCLOSE:
Karsten Hopp 4e0c26
! 	    subs = addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
  	    break;
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	case NFA_MOPEN:
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 4094,4100 ****
Karsten Hopp 4e0c26
  		sub->list.line[subidx].start = reginput + off;
Karsten Hopp 4e0c26
  	    }
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
! 	    addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	    if (save_in_use == -1)
Karsten Hopp 4e0c26
  	    {
Karsten Hopp 4e0c26
--- 4113,4119 ----
Karsten Hopp 4e0c26
  		sub->list.line[subidx].start = reginput + off;
Karsten Hopp 4e0c26
  	    }
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
! 	    subs = addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	    if (save_in_use == -1)
Karsten Hopp 4e0c26
  	    {
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 4112,4118 ****
Karsten Hopp 4e0c26
  	    {
Karsten Hopp 4e0c26
  		/* Do not overwrite the position set by \ze. If no \ze
Karsten Hopp 4e0c26
  		 * encountered end will be set in nfa_regtry(). */
Karsten Hopp 4e0c26
! 		addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
  		break;
Karsten Hopp 4e0c26
  	    }
Karsten Hopp 4e0c26
  	case NFA_MCLOSE1:
Karsten Hopp 4e0c26
--- 4131,4137 ----
Karsten Hopp 4e0c26
  	    {
Karsten Hopp 4e0c26
  		/* Do not overwrite the position set by \ze. If no \ze
Karsten Hopp 4e0c26
  		 * encountered end will be set in nfa_regtry(). */
Karsten Hopp 4e0c26
! 		subs = addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
  		break;
Karsten Hopp 4e0c26
  	    }
Karsten Hopp 4e0c26
  	case NFA_MCLOSE1:
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 4181,4187 ****
Karsten Hopp 4e0c26
  		sub->list.line[subidx].end = reginput + off;
Karsten Hopp 4e0c26
  	    }
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
! 	    addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	    if (REG_MULTI)
Karsten Hopp 4e0c26
  		sub->list.multi[subidx].end = save_lpos;
Karsten Hopp 4e0c26
--- 4200,4206 ----
Karsten Hopp 4e0c26
  		sub->list.line[subidx].end = reginput + off;
Karsten Hopp 4e0c26
  	    }
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
! 	    subs = addstate(l, state->out, subs, pim, off);
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  	    if (REG_MULTI)
Karsten Hopp 4e0c26
  		sub->list.multi[subidx].end = save_lpos;
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 4190,4195 ****
Karsten Hopp 4e0c26
--- 4209,4215 ----
Karsten Hopp 4e0c26
  	    sub->in_use = save_in_use;
Karsten Hopp 4e0c26
  	    break;
Karsten Hopp 4e0c26
      }
Karsten Hopp 4e0c26
+     return subs;
Karsten Hopp 4e0c26
  }
Karsten Hopp 4e0c26
  
Karsten Hopp 4e0c26
  /*
Karsten Hopp 4e0c26
*** ../vim-7.3.1279/src/version.c	2013-06-30 22:43:22.000000000 +0200
Karsten Hopp 4e0c26
--- src/version.c	2013-06-30 23:23:02.000000000 +0200
Karsten Hopp 4e0c26
***************
Karsten Hopp 4e0c26
*** 730,731 ****
Karsten Hopp 4e0c26
--- 730,733 ----
Karsten Hopp 4e0c26
  {   /* Add new patch number below this line */
Karsten Hopp 4e0c26
+ /**/
Karsten Hopp 4e0c26
+     1280,
Karsten Hopp 4e0c26
  /**/
Karsten Hopp 4e0c26
Karsten Hopp 4e0c26
-- 
Karsten Hopp 4e0c26
DENNIS:  Listen -- strange women lying in ponds distributing swords is no
Karsten Hopp 4e0c26
         basis for a system of government.  Supreme executive power derives
Karsten Hopp 4e0c26
         from a mandate from the masses, not from some farcical aquatic
Karsten Hopp 4e0c26
         ceremony.
Karsten Hopp 4e0c26
                                  The Quest for the Holy Grail (Monty Python)
Karsten Hopp 4e0c26
Karsten Hopp 4e0c26
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp 4e0c26
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp 4e0c26
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp 4e0c26
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///