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