home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / patches / 7.4 / 7.4.560 < prev    next >
Encoding:
Internet Message Format  |  2014-12-17  |  5.2 KB

  1. To: vim_dev@googlegroups.com
  2. Subject: Patch 7.4.560
  3. Fcc: outbox
  4. From: Bram Moolenaar <Bram@moolenaar.net>
  5. Mime-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. ------------
  9.  
  10. Patch 7.4.560
  11. Problem:    Memory leak using :wviminfo. Issue 296.
  12. Solution:   Free memory when needed. (idea by Christian Brabandt)
  13. Files:        src/ops.c
  14.  
  15.  
  16. *** ../vim-7.4.559/src/ops.c    2014-12-17 18:35:37.553795955 +0100
  17. --- src/ops.c    2014-12-17 20:59:49.722557613 +0100
  18. ***************
  19. *** 5663,5668 ****
  20. --- 5663,5670 ----
  21.       int        set_prev = FALSE;
  22.       char_u    *str;
  23.       char_u    **array = NULL;
  24. +     int        new_type;
  25. +     colnr_T    new_width;
  26.   
  27.       /* We only get here (hopefully) if line[0] == '"' */
  28.       str = virp->vir_line + 1;
  29. ***************
  30. *** 5695,5715 ****
  31.       limit = 100;    /* Optimized for registers containing <= 100 lines */
  32.       if (do_it)
  33.       {
  34.       if (set_prev)
  35.           y_previous = y_current;
  36. !     vim_free(y_current->y_array);
  37. !     array = y_current->y_array =
  38. !                (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
  39.       str = skipwhite(skiptowhite(str));
  40.       if (STRNCMP(str, "CHAR", 4) == 0)
  41. !         y_current->y_type = MCHAR;
  42.       else if (STRNCMP(str, "BLOCK", 5) == 0)
  43. !         y_current->y_type = MBLOCK;
  44.       else
  45. !         y_current->y_type = MLINE;
  46.       /* get the block width; if it's missing we get a zero, which is OK */
  47.       str = skipwhite(skiptowhite(str));
  48. !     y_current->y_width = getdigits(&str);
  49.       }
  50.   
  51.       while (!(eof = viminfo_readline(virp))
  52. --- 5697,5721 ----
  53.       limit = 100;    /* Optimized for registers containing <= 100 lines */
  54.       if (do_it)
  55.       {
  56. +     /*
  57. +      * Build the new register in array[].
  58. +      * y_array is kept as-is until done.
  59. +      * The "do_it" flag is reset when something is wrong, in which case
  60. +      * array[] needs to be freed.
  61. +      */
  62.       if (set_prev)
  63.           y_previous = y_current;
  64. !     array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
  65.       str = skipwhite(skiptowhite(str));
  66.       if (STRNCMP(str, "CHAR", 4) == 0)
  67. !         new_type = MCHAR;
  68.       else if (STRNCMP(str, "BLOCK", 5) == 0)
  69. !         new_type = MBLOCK;
  70.       else
  71. !         new_type = MLINE;
  72.       /* get the block width; if it's missing we get a zero, which is OK */
  73.       str = skipwhite(skiptowhite(str));
  74. !     new_width = getdigits(&str);
  75.       }
  76.   
  77.       while (!(eof = viminfo_readline(virp))
  78. ***************
  79. *** 5717,5756 ****
  80.       {
  81.       if (do_it)
  82.       {
  83. !         if (size >= limit)
  84.           {
  85. !         y_current->y_array = (char_u **)
  86.                     alloc((unsigned)(limit * 2 * sizeof(char_u *)));
  87.           for (i = 0; i < limit; i++)
  88. !             y_current->y_array[i] = array[i];
  89.           vim_free(array);
  90.           limit *= 2;
  91. -         array = y_current->y_array;
  92.           }
  93.           str = viminfo_readstring(virp, 1, TRUE);
  94.           if (str != NULL)
  95.           array[size++] = str;
  96.           else
  97.           do_it = FALSE;
  98.       }
  99.       }
  100.       if (do_it)
  101.       {
  102.       if (size == 0)
  103.       {
  104. -         vim_free(array);
  105.           y_current->y_array = NULL;
  106.       }
  107. !     else if (size < limit)
  108.       {
  109.           y_current->y_array =
  110.               (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
  111.           for (i = 0; i < size; i++)
  112. !         y_current->y_array[i] = array[i];
  113. !         vim_free(array);
  114.       }
  115. -     y_current->y_size = size;
  116.       }
  117.       return eof;
  118.   }
  119.   
  120. --- 5723,5788 ----
  121.       {
  122.       if (do_it)
  123.       {
  124. !         if (size == limit)
  125.           {
  126. !         char_u **new_array = (char_u **)
  127.                     alloc((unsigned)(limit * 2 * sizeof(char_u *)));
  128. +         if (new_array == NULL)
  129. +         {
  130. +             do_it = FALSE;
  131. +             break;
  132. +         }
  133.           for (i = 0; i < limit; i++)
  134. !             new_array[i] = array[i];
  135.           vim_free(array);
  136. +         array = new_array;
  137.           limit *= 2;
  138.           }
  139.           str = viminfo_readstring(virp, 1, TRUE);
  140.           if (str != NULL)
  141.           array[size++] = str;
  142.           else
  143. +         /* error, don't store the result */
  144.           do_it = FALSE;
  145.       }
  146.       }
  147.       if (do_it)
  148.       {
  149. +     /* free y_array[] */
  150. +     for (i = 0; i < y_current->y_size; i++)
  151. +         vim_free(y_current->y_array[i]);
  152. +     vim_free(y_current->y_array);
  153. +     y_current->y_type = new_type;
  154. +     y_current->y_width = new_width;
  155. +     y_current->y_size = size;
  156.       if (size == 0)
  157.       {
  158.           y_current->y_array = NULL;
  159.       }
  160. !     else
  161.       {
  162. +         /* Move the lines from array[] to y_array[]. */
  163.           y_current->y_array =
  164.               (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
  165.           for (i = 0; i < size; i++)
  166. !         {
  167. !         if (y_current->y_array == NULL)
  168. !             vim_free(array[i]);
  169. !         else
  170. !             y_current->y_array[i] = array[i];
  171. !         }
  172.       }
  173.       }
  174. +     else
  175. +     {
  176. +     /* Free array[] if it was filled. */
  177. +     for (i = 0; i < size; i++)
  178. +         vim_free(array[i]);
  179. +     }
  180. +     vim_free(array);
  181.       return eof;
  182.   }
  183.   
  184. *** ../vim-7.4.559/src/version.c    2014-12-17 18:35:37.553795955 +0100
  185. --- src/version.c    2014-12-17 18:56:33.810259558 +0100
  186. ***************
  187. *** 743,744 ****
  188. --- 743,746 ----
  189.   {   /* Add new patch number below this line */
  190. + /**/
  191. +     560,
  192.   /**/
  193.  
  194. -- 
  195. hundred-and-one symptoms of being an internet addict:
  196. 17. You turn on your intercom when leaving the room so you can hear if new
  197.     e-mail arrives.
  198.  
  199.  /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
  200. ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
  201. \\\  an exciting new programming language -- http://www.Zimbu.org        ///
  202.  \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///
  203.