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

Re: Somewhat unexpected results of {myfd}>&1 when noclobber set



On Sun, 10 Mar 2013 18:52:04 +0000
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Sat, 09 Mar 2013 07:12:52 -0800
> Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> > On Mar 9, 11:33am, Mikael Magnusson wrote:
> > }
> > } % bar=( 14 15 )
> > } % : {bar}>&1
> > } zsh: bad math expression: operator expected at `15'
> > } % echo $bar
> > } 16
> > 
> > This is clearly a bug.  Failing the math eval should either abort the
> > entire operation, or not print the warning.  I think the latter would
> > be considered the intended behavior, that is, bar does not contain a
> > descriptor so it simply gets assigned a new one by the redirection.
> 
> I suppose so
>...
> But I think Michael's right that to get that effect we don't want the
> matheval at all --- we just want to see if foo contains an integer (it
> looks like we also check if it's a previously opened fd after retrieving it).

Here's my go.

Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.215
diff -p -u -r1.215 exec.c
--- Src/exec.c	13 Dec 2012 10:37:00 -0000	1.215
+++ Src/exec.c	10 Mar 2013 20:16:44 -0000
@@ -1885,7 +1885,14 @@ checkclobberparam(struct redir *f)
 	return 0;
     }
 
-    if (!isset(CLOBBER) && (fd = (int)getintvalue(v)) &&
+    /*
+     * We can't clobber the value in the parameter if it's
+     * already an opened file descriptor --- that means it's a decimal
+     * integer corresponding to an opened file descriptor,
+     * not merely an expression that evaluates to a file descriptor.
+     */
+    if (!isset(CLOBBER) && (s = getstrvalue(v)) &&
+	(fd = (int)zstrtol(s, &s, 10)) >= 0 && !*s &&
 	fd <= max_zsh_fd && fdtable[fd] == FDT_EXTERNAL) {
 	zwarn("can't clobber parameter %s containing file descriptor %d",
 	     f->varid, fd);
Index: Test/A04redirect.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A04redirect.ztst,v
retrieving revision 1.25
diff -p -u -r1.25 A04redirect.ztst
--- Test/A04redirect.ztst	15 Nov 2012 21:08:16 -0000	1.25
+++ Test/A04redirect.ztst	10 Mar 2013 20:16:44 -0000
@@ -328,6 +328,17 @@
 1q:NO_CLOBBER prevents overwriting parameter with allocated fd
 ?(eval):4: can't clobber parameter myfd containing file descriptor $myfd
 
+  (setopt noclobber
+   exec {myfd}>logfile2b
+   print First open >&$myfd
+   rm -f logfile2b # prevent normal file no_clobberation
+   myotherfd="${myfd}+0"
+   exec {myotherfd}>logfile2b
+   print Overwritten >&$myotherfd)
+   cat logfile2b
+0:NO_CLOBBER doesn't complain about any other expression
+>Overwritten
+
   (exec {myfd}>logfile4
   echo $myfd
   exec {myfd}>&-

-- 
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