Blame SOURCES/7.4.560

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