3ef2ca
To: vim_dev@googlegroups.com
3ef2ca
Subject: Patch 7.4.560
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.560
3ef2ca
Problem:    Memory leak using :wviminfo. Issue 296.
3ef2ca
Solution:   Free memory when needed. (idea by Christian Brabandt)
3ef2ca
Files:	    src/ops.c
3ef2ca
3ef2ca
3ef2ca
*** ../vim-7.4.559/src/ops.c	2014-12-17 18:35:37.553795955 +0100
3ef2ca
--- src/ops.c	2014-12-17 20:59:49.722557613 +0100
3ef2ca
***************
3ef2ca
*** 5663,5668 ****
3ef2ca
--- 5663,5670 ----
3ef2ca
      int		set_prev = FALSE;
3ef2ca
      char_u	*str;
3ef2ca
      char_u	**array = NULL;
3ef2ca
+     int		new_type;
3ef2ca
+     colnr_T	new_width;
3ef2ca
  
3ef2ca
      /* We only get here (hopefully) if line[0] == '"' */
3ef2ca
      str = virp->vir_line + 1;
3ef2ca
***************
3ef2ca
*** 5695,5715 ****
3ef2ca
      limit = 100;	/* Optimized for registers containing <= 100 lines */
3ef2ca
      if (do_it)
3ef2ca
      {
3ef2ca
  	if (set_prev)
3ef2ca
  	    y_previous = y_current;
3ef2ca
! 	vim_free(y_current->y_array);
3ef2ca
! 	array = y_current->y_array =
3ef2ca
! 		       (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
3ef2ca
  	str = skipwhite(skiptowhite(str));
3ef2ca
  	if (STRNCMP(str, "CHAR", 4) == 0)
3ef2ca
! 	    y_current->y_type = MCHAR;
3ef2ca
  	else if (STRNCMP(str, "BLOCK", 5) == 0)
3ef2ca
! 	    y_current->y_type = MBLOCK;
3ef2ca
  	else
3ef2ca
! 	    y_current->y_type = MLINE;
3ef2ca
  	/* get the block width; if it's missing we get a zero, which is OK */
3ef2ca
  	str = skipwhite(skiptowhite(str));
3ef2ca
! 	y_current->y_width = getdigits(&str);
3ef2ca
      }
3ef2ca
  
3ef2ca
      while (!(eof = viminfo_readline(virp))
3ef2ca
--- 5697,5721 ----
3ef2ca
      limit = 100;	/* Optimized for registers containing <= 100 lines */
3ef2ca
      if (do_it)
3ef2ca
      {
3ef2ca
+ 	/*
3ef2ca
+ 	 * Build the new register in array[].
3ef2ca
+ 	 * y_array is kept as-is until done.
3ef2ca
+ 	 * The "do_it" flag is reset when something is wrong, in which case
3ef2ca
+ 	 * array[] needs to be freed.
3ef2ca
+ 	 */
3ef2ca
  	if (set_prev)
3ef2ca
  	    y_previous = y_current;
3ef2ca
! 	array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
3ef2ca
  	str = skipwhite(skiptowhite(str));
3ef2ca
  	if (STRNCMP(str, "CHAR", 4) == 0)
3ef2ca
! 	    new_type = MCHAR;
3ef2ca
  	else if (STRNCMP(str, "BLOCK", 5) == 0)
3ef2ca
! 	    new_type = MBLOCK;
3ef2ca
  	else
3ef2ca
! 	    new_type = MLINE;
3ef2ca
  	/* get the block width; if it's missing we get a zero, which is OK */
3ef2ca
  	str = skipwhite(skiptowhite(str));
3ef2ca
! 	new_width = getdigits(&str);
3ef2ca
      }
3ef2ca
  
3ef2ca
      while (!(eof = viminfo_readline(virp))
3ef2ca
***************
3ef2ca
*** 5717,5756 ****
3ef2ca
      {
3ef2ca
  	if (do_it)
3ef2ca
  	{
3ef2ca
! 	    if (size >= limit)
3ef2ca
  	    {
3ef2ca
! 		y_current->y_array = (char_u **)
3ef2ca
  			      alloc((unsigned)(limit * 2 * sizeof(char_u *)));
3ef2ca
  		for (i = 0; i < limit; i++)
3ef2ca
! 		    y_current->y_array[i] = array[i];
3ef2ca
  		vim_free(array);
3ef2ca
  		limit *= 2;
3ef2ca
- 		array = y_current->y_array;
3ef2ca
  	    }
3ef2ca
  	    str = viminfo_readstring(virp, 1, TRUE);
3ef2ca
  	    if (str != NULL)
3ef2ca
  		array[size++] = str;
3ef2ca
  	    else
3ef2ca
  		do_it = FALSE;
3ef2ca
  	}
3ef2ca
      }
3ef2ca
      if (do_it)
3ef2ca
      {
3ef2ca
  	if (size == 0)
3ef2ca
  	{
3ef2ca
- 	    vim_free(array);
3ef2ca
  	    y_current->y_array = NULL;
3ef2ca
  	}
3ef2ca
! 	else if (size < limit)
3ef2ca
  	{
3ef2ca
  	    y_current->y_array =
3ef2ca
  			(char_u **)alloc((unsigned)(size * sizeof(char_u *)));
3ef2ca
  	    for (i = 0; i < size; i++)
3ef2ca
! 		y_current->y_array[i] = array[i];
3ef2ca
! 	    vim_free(array);
3ef2ca
  	}
3ef2ca
- 	y_current->y_size = size;
3ef2ca
      }
3ef2ca
      return eof;
3ef2ca
  }
3ef2ca
  
3ef2ca
--- 5723,5788 ----
3ef2ca
      {
3ef2ca
  	if (do_it)
3ef2ca
  	{
3ef2ca
! 	    if (size == limit)
3ef2ca
  	    {
3ef2ca
! 		char_u **new_array = (char_u **)
3ef2ca
  			      alloc((unsigned)(limit * 2 * sizeof(char_u *)));
3ef2ca
+ 
3ef2ca
+ 		if (new_array == NULL)
3ef2ca
+ 		{
3ef2ca
+ 		    do_it = FALSE;
3ef2ca
+ 		    break;
3ef2ca
+ 		}
3ef2ca
  		for (i = 0; i < limit; i++)
3ef2ca
! 		    new_array[i] = array[i];
3ef2ca
  		vim_free(array);
3ef2ca
+ 		array = new_array;
3ef2ca
  		limit *= 2;
3ef2ca
  	    }
3ef2ca
  	    str = viminfo_readstring(virp, 1, TRUE);
3ef2ca
  	    if (str != NULL)
3ef2ca
  		array[size++] = str;
3ef2ca
  	    else
3ef2ca
+ 		/* error, don't store the result */
3ef2ca
  		do_it = FALSE;
3ef2ca
  	}
3ef2ca
      }
3ef2ca
      if (do_it)
3ef2ca
      {
3ef2ca
+ 	/* free y_array[] */
3ef2ca
+ 	for (i = 0; i < y_current->y_size; i++)
3ef2ca
+ 	    vim_free(y_current->y_array[i]);
3ef2ca
+ 	vim_free(y_current->y_array);
3ef2ca
+ 
3ef2ca
+ 	y_current->y_type = new_type;
3ef2ca
+ 	y_current->y_width = new_width;
3ef2ca
+ 	y_current->y_size = size;
3ef2ca
  	if (size == 0)
3ef2ca
  	{
3ef2ca
  	    y_current->y_array = NULL;
3ef2ca
  	}
3ef2ca
! 	else
3ef2ca
  	{
3ef2ca
+ 	    /* Move the lines from array[] to y_array[]. */
3ef2ca
  	    y_current->y_array =
3ef2ca
  			(char_u **)alloc((unsigned)(size * sizeof(char_u *)));
3ef2ca
  	    for (i = 0; i < size; i++)
3ef2ca
! 	    {
3ef2ca
! 		if (y_current->y_array == NULL)
3ef2ca
! 		    vim_free(array[i]);
3ef2ca
! 		else
3ef2ca
! 		    y_current->y_array[i] = array[i];
3ef2ca
! 	    }
3ef2ca
  	}
3ef2ca
      }
3ef2ca
+     else
3ef2ca
+     {
3ef2ca
+ 	/* Free array[] if it was filled. */
3ef2ca
+ 	for (i = 0; i < size; i++)
3ef2ca
+ 	    vim_free(array[i]);
3ef2ca
+     }
3ef2ca
+     vim_free(array);
3ef2ca
+ 
3ef2ca
      return eof;
3ef2ca
  }
3ef2ca
  
3ef2ca
*** ../vim-7.4.559/src/version.c	2014-12-17 18:35:37.553795955 +0100
3ef2ca
--- src/version.c	2014-12-17 18:56:33.810259558 +0100
3ef2ca
***************
3ef2ca
*** 743,744 ****
3ef2ca
--- 743,746 ----
3ef2ca
  {   /* Add new patch number below this line */
3ef2ca
+ /**/
3ef2ca
+     560,
3ef2ca
  /**/
3ef2ca
3ef2ca
-- 
3ef2ca
hundred-and-one symptoms of being an internet addict:
3ef2ca
17. You turn on your intercom when leaving the room so you can hear if new
3ef2ca
    e-mail arrives.
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    ///