zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh workers <zsh-workers@zsh.org>
Subject: Re: Somewhat unexpected results of {myfd}>&1 when noclobber set
Date: Sun, 10 Mar 2013 20:17:41 +0000	[thread overview]
Message-ID: <20130310201741.0658434b@pws-pc.ntlworld.com> (raw)
In-Reply-To: <20130310185204.47d755e8@pws-pc.ntlworld.com>

On Sun, 10 Mar 2013 18:52:04 +0000
Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> On Sat, 09 Mar 2013 07:12:52 -0800
> Bart Schaefer <schaefer@brasslantern.com> 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@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


      reply	other threads:[~2013-03-10 20:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-09 10:33 Mikael Magnusson
2013-03-09 15:12 ` Bart Schaefer
2013-03-10 12:11   ` Mikael Magnusson
2013-03-10 18:26     ` Bart Schaefer
2013-03-10 18:52   ` Peter Stephenson
2013-03-10 20:17     ` Peter Stephenson [this message]

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=20130310201741.0658434b@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.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).