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

Re: [MAYBE BUG] resetting `$0' in sourced script causes zsh to crash on Debian



On Fri, 6 Nov 2009 19:49:31 +0100
Stefano Lattarini <stefano.lattarini@xxxxxxxxx> wrote:
> I have recently found a problem in resetting `$0' inside a script "sourced"
> with the `.' builtin.
> 
> Not sure if it's a bug or if it's just me doing something that should not
> be done; hopefully you can tell.

Yep, that's a bug.  Thanks for the clear instructions.  The patch makes
valgrind happy, and fits with your description.

(If the shell didn't want you to set $0, it would declare it readonly
and tell you to go it way when you set it.  Since it doesn't you should
be able to set it, and in either case there shouldn't be a memory error.)

By the way, anyone running valgrind is invited to look at the errors in
the code reported.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.235
diff -u -r1.235 builtin.c
--- Src/builtin.c	2 Sep 2009 15:27:35 -0000	1.235
+++ Src/builtin.c	6 Nov 2009 21:26:06 -0000
@@ -4747,7 +4747,7 @@
     enam = arg0 = ztrdup(*argv);
     if (isset(FUNCTIONARGZERO)) {
 	old0 = argzero;
-	argzero = arg0;
+	argzero = ztrdup(arg0);
     }
     s = unmeta(enam);
     errno = ENOENT;
@@ -4802,8 +4802,10 @@
     if (ret == SOURCE_NOT_FOUND)
 	zwarnnam(name, "%e: %s", errno, enam);
     zsfree(arg0);
-    if (old0)
+    if (old0) {
+	zsfree(argzero);
 	argzero = old0;
+    }
     return ret == SOURCE_OK ? lastval : 128 - ret;
 }
 
-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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