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