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

Re: PATCH: Allow setting $0 when POSIX_ARGZERO is not set



On Tue, Jun 16, 2015 at 5:50 AM, Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Jun 16,  1:44am, Mikael Magnusson wrote:
> }
> } I don't think this should hurt anything.
>
> When sourcing a script the global argzero (which has been initialized
> from runscript) is not malloc'd memory.
>
> ==25941== Invalid free() / delete / delete[]
> ==25941==    at 0x4004EFA: free (vg_replace_malloc.c:235)
> ==25941==    by 0x80950D7: zsfree (mem.c:1828)
> ==25941==    by 0x80A4DE9: argzerosetfn (params.c:4055)
> ==25941==    by 0x80A0CDF: assignstrvalue (params.c:2349)
> ==25941==    by 0x80A22D2: assignsparam (params.c:2806)
> ==25941==    by 0x806642F: addvars (exec.c:2317)
> ==25941==    by 0x80635F0: execsimple (exec.c:1117)
> ==25941==    by 0x80639AD: execlist (exec.c:1247)
> ==25941==    by 0x80634D1: execode (exec.c:1074)
> ==25941==    by 0x807F4EC: loop (init.c:207)
> ==25941==    by 0x808290B: zsh_main (init.c:1674)
> ==25941==    by 0x804C2E9: main (main.c:93)
> ==25941==  Address 0xBEEAFA62 is on thread 1's stack
>
> I'm not sure if this comes up anywhere else.  I wasn't trivially able
> to make it fail.
>
> I think it would cause leaks to have parseargs() call ztrdup() for its
> runscript argument, so it looks like setupshin() should do so when
> assigning to argzero.
>
> There are probably other race conditions if a signal were to arrive
> while the shell is still initializing state.  Maybe we should get a
> queue_signals() in there somewhere early.

Oh, in that case maybe it's a lot more trouble than it's worth...

> diff --git a/Src/init.c b/Src/init.c
> index 102276a..0fe4d75 100644
> --- a/Src/init.c
> +++ b/Src/init.c
> @@ -1117,8 +1117,9 @@ setupshin(char *runscript)
>             exit(127);
>         }
>         scriptfilename = sfname;
> -       zsfree(argzero); /* ztrdup'd in parseargs */
> -       argzero = runscript;
> +       sfname = argzero; /* copy to avoid race condition */
> +       argzero = ztrdup(runscript);
> +       zsfree(sfname); /* argzero ztrdup'd in parseargs */
>      }
>      /*
>       * We only initialise line numbering once there is a script to



-- 
Mikael Magnusson



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