Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: zsh-3.1.5-pws-4 + typeset fix: memory leak



"Daniel X. Pape" wrote:
> On Thu, 14 Jan 1999, Daniel X. Pape wrote:
> 
> > I recently downloaded and compiled zsh-3.1.5-pws-4. However, I found a
> > couple of memory leaks in builtin.c where the result of a ztrdup was
> > being used as an argument, and not freed, and also where the result of a
> > "promptexpand" was being passed to "unmetafy," and then that result was
> > never freed.
> 
> Here is the patch - let me know if it helps:

You're certainly right about the memory leaks --- thanks.  I've
rewritten the patch a bit: the first part now fits in with a recent
patch of mine in zsh-workers/4902, and also I don't think you actually
need to duplicate the parameter name at all (somebody must simply have
forgotten that createparam() does that inside).  In the second case,
the problem with your approach is that `print -P' can take more than
one argument, and all need freeing: the easiest thing to do is use
heap memory (which gets freed automatically when the builtin exits),
and free the permanent memory straight away.

*** Src/builtin.c.mem	Thu Jan 14 15:55:34 1999
--- Src/builtin.c	Fri Jan 15 16:29:33 1999
***************
*** 1560,1566 ****
       * Create a new node for a parameter with the flags in `on' minus the
       * readonly flag
       */
!     pm = createparam(ztrdup(pname), on & ~PM_READONLY);
      DPUTS(!pm, "BUG: parameter not created");
      pm->ct = auxlen;
      if (func != BIN_EXPORT)
--- 1560,1566 ----
       * Create a new node for a parameter with the flags in `on' minus the
       * readonly flag
       */
!     pm = createparam(pname, on & ~PM_READONLY);
      DPUTS(!pm, "BUG: parameter not created");
      pm->ct = auxlen;
      if (func != BIN_EXPORT)
***************
*** 2346,2354 ****
  	    args[n] = getkeystring(args[n], &len[n],
  				    func != BIN_ECHO && !ops['e'], &nnl);
  	/* -P option -- interpret as a prompt sequence */
! 	if(ops['P'])
! 	    args[n] = unmetafy(promptexpand(metafy(args[n], len[n],
! 		META_NOALLOC), 0, NULL, NULL), &len[n]);
  	/* -D option -- interpret as a directory, and use ~ */
  	if(ops['D']) {
  	    Nameddir d = finddir(args[n]);
--- 2346,2362 ----
  	    args[n] = getkeystring(args[n], &len[n],
  				    func != BIN_ECHO && !ops['e'], &nnl);
  	/* -P option -- interpret as a prompt sequence */
! 	if(ops['P']) {
! 	    /*
! 	     * promptexpand uses permanent storage: to avoid
! 	     * messy memory management, stick it on the heap
! 	     * instead.
! 	     */
! 	    char *str = unmetafy(promptexpand(metafy(args[n], len[n],
! 				   META_NOALLOC), 0, NULL, NULL), &len[n]);
! 	    args[n] = dupstring(str);
! 	    free(str);
! 	}
  	/* -D option -- interpret as a directory, and use ~ */
  	if(ops['D']) {
  	    Nameddir d = finddir(args[n]);

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxx>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy



Messages sorted by: Reverse Date, Date, Thread, Author