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

Re: hzoli change: $foo:s//r/



> 
> Another very minor problem with Zoltan's substitution code.
> 
> If hsubr is non-NULL, and a "bad/no previous substitution" error 
> occurs, hsubr is free()d. This may corrupt memory. Example:
> 
>   $ PS1='[%!] ' ./zsh -f
>   [1] echo $foo:s/x//
>   
>   [2] echo $foo:s/ //
>   zsh: bad substitution
>   [3] echo $foo:s/ //
>   zsh: attempt to free already free storage
> 
> This fix only free()s hsubr when there was *no* error.

You are right here.  But there is an other problem: the code assumes in many
places that extern variables are initialized to zero.  If hsubl/hsubr were not
zero on startup it would cause the same problem even after this patch.  The
patch below initializes hsubl/hsubr in globals.h and probably there are some
other variables which require similar treatment.

Zoltan

*** 1.10	1995/07/24 17:12:46
--- Src/globals.h	1995/08/14 17:09:11
***************
*** 36,43 ****
--- 36,45 ----
  
  #ifdef GLOBALS
  # define EXTERN
+ # define CHARPTR(X) char *X = NULL;
  #else
  # define EXTERN extern
+ # define CHARPTR(X) extern char *X;
  #endif
   
  #ifdef GLOBALS
***************
*** 284,294 ****
   
  /* the last l for s/l/r/ history substitution */
   
! EXTERN char *hsubl;
  
  /* the last r for s/l/r/ history substitution */
   
! EXTERN char *hsubr;
   
  /* We cache `USERNAME' and use check cached_uid *
   * so we know when to recompute it.             */
--- 286,296 ----
   
  /* the last l for s/l/r/ history substitution */
   
! CHARPTR(hsubl)
  
  /* the last r for s/l/r/ history substitution */
   
! CHARPTR(hsubr)
   
  /* We cache `USERNAME' and use check cached_uid *
   * so we know when to recompute it.             */



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