zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Subject: Re: PATCH: Allow setting $0 when POSIX_ARGZERO is not set
Date: Mon, 15 Jun 2015 20:50:44 -0700	[thread overview]
Message-ID: <150615205044.ZM25959@torch.brasslantern.com> (raw)
In-Reply-To: <1434411879-12403-1-git-send-email-mikachu@gmail.com>

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.


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


  reply	other threads:[~2015-06-16  3:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-15 23:44 Mikael Magnusson
2015-06-16  3:50 ` Bart Schaefer [this message]
2015-06-16  6:25   ` Mikael Magnusson
2015-06-16 15:58     ` Bart Schaefer
2015-06-16 23:46       ` Mikael Magnusson
2015-06-17  0:12         ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=150615205044.ZM25959@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).