zsh-workers
 help / color / mirror / code / Atom feed
* xtrace output sets ERRNO to 9 (EBADF)
@ 2022-12-09 14:10 Stephane Chazelas
  2022-12-10  2:57 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Stephane Chazelas @ 2022-12-09 14:10 UTC (permalink / raw)
  To: Zsh hackers list

$ ./Src/zsh -c 'set -o xtrace; ERRNO=0; :; echo "$ERRNO"'
+zsh:1> ERRNO=0
+zsh:1> :
+zsh:1> echo 9
9

Seems to be caused by a double-close. From strace:

dup(2)                                  = 3
fcntl(3, F_DUPFD, 10)                   = 12
close(3)                                = 0
fcntl(12, F_GETFL)                      = 0x1 (flags O_WRONLY)
newfstatat(12, "", {st_mode=S_IFIFO|0600, st_size=0, ...}, AT_EMPTY_PATH) = 0
brk(0x557861189000)                     = 0x557861189000
write(12, "+zsh:1> :\n", 10+zsh:1> :
)            = 10
close(12)                               = 0
brk(0x557861188000)                     = 0x557861188000
close(12)                               = -1 EBADF (Bad file descriptor)

(on Debian GNU/Linux amd64 with 5.9 or git HEAD).

-- 
Stephane


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: xtrace output sets ERRNO to 9 (EBADF)
  2022-12-09 14:10 xtrace output sets ERRNO to 9 (EBADF) Stephane Chazelas
@ 2022-12-10  2:57 ` Bart Schaefer
  2022-12-10 11:38   ` Daniel Shahaf
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2022-12-10  2:57 UTC (permalink / raw)
  To: Zsh hackers list

diff --git a/Src/exec.c b/Src/exec.c
index 1810fca5e..a1059af5e 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -4336,10 +4336,13 @@ execcmd_exec(Estate state, Execcmd_params eparams,
     }
     }
     if (newxtrerr) {
+    int eno = errno;
     fil = fileno(newxtrerr);
     fclose(newxtrerr);
     xtrerr = oxtrerr;
+    /* Call zclose() to clean up internal tables, ignore EBADF */
     zclose(fil);
+    errno = eno;
     }

     zsfree(STTYval);


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: xtrace output sets ERRNO to 9 (EBADF)
  2022-12-10  2:57 ` Bart Schaefer
@ 2022-12-10 11:38   ` Daniel Shahaf
  2022-12-11 18:35     ` Stephane Chazelas
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Shahaf @ 2022-12-10 11:38 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer wrote on Fri, Dec 09, 2022 at 18:57:37 -0800:
> diff --git a/Src/exec.c b/Src/exec.c
> index 1810fca5e..a1059af5e 100644
> --- a/Src/exec.c
> +++ b/Src/exec.c
> @@ -4336,10 +4336,13 @@ execcmd_exec(Estate state, Execcmd_params eparams,
>      }
>      }
>      if (newxtrerr) {
> +    int eno = errno;
>      fil = fileno(newxtrerr);
>      fclose(newxtrerr);
>      xtrerr = oxtrerr;
> +    /* Call zclose() to clean up internal tables, ignore EBADF */
>      zclose(fil);
> +    errno = eno;

This ignores any errors from the fileno() and fclose() calls as well, though?

>      }
> 
>      zsfree(STTYval);
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: xtrace output sets ERRNO to 9 (EBADF)
  2022-12-10 11:38   ` Daniel Shahaf
@ 2022-12-11 18:35     ` Stephane Chazelas
  2022-12-11 18:52       ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Stephane Chazelas @ 2022-12-11 18:35 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-workers

2022-12-10 11:38:13 +0000, Daniel Shahaf:
> Bart Schaefer wrote on Fri, Dec 09, 2022 at 18:57:37 -0800:
> > diff --git a/Src/exec.c b/Src/exec.c
> > index 1810fca5e..a1059af5e 100644
> > --- a/Src/exec.c
> > +++ b/Src/exec.c
> > @@ -4336,10 +4336,13 @@ execcmd_exec(Estate state, Execcmd_params eparams,
> >      }
> >      }
> >      if (newxtrerr) {
> > +    int eno = errno;
> >      fil = fileno(newxtrerr);
> >      fclose(newxtrerr);
> >      xtrerr = oxtrerr;
> > +    /* Call zclose() to clean up internal tables, ignore EBADF */
> >      zclose(fil);
> > +    errno = eno;
> 
> This ignores any errors from the fileno() and fclose() calls as well, though?
[...]

Yes, though it's probably just  as well. Users use $ERRNO to
detect errors while doing operations they asked for.

In my case, I was doing:

ERRNO=0
files=(**/someglob(N))
if (( ERRNO )) ...

To check for errors during the glob expansion (wanting to make
sure that it doesn't missing anythin).

And finding that that glob expansion seemingly failed with EBADF
when run under zsh -x.

-- 
Stephane


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: xtrace output sets ERRNO to 9 (EBADF)
  2022-12-11 18:35     ` Stephane Chazelas
@ 2022-12-11 18:52       ` Bart Schaefer
  2022-12-11 20:01         ` Stephane Chazelas
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2022-12-11 18:52 UTC (permalink / raw)
  To: Daniel Shahaf, zsh-workers

On Sun, Dec 11, 2022 at 10:36 AM Stephane Chazelas
<stephane@chazelas.org> wrote:
>
> > This ignores any errors from the fileno() and fclose() calls as well, though?
>
> Yes, though it's probably just  as well. Users use $ERRNO to
> detect errors while doing operations they asked for.

Exactly; xtrace shouldn't be affecting program errors.  In any case,
fileno() doesn't set errno (it's just a data structure deref, would
only fail by crashing) and zclose() already clobbers the error from
fclose().


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: xtrace output sets ERRNO to 9 (EBADF)
  2022-12-11 18:52       ` Bart Schaefer
@ 2022-12-11 20:01         ` Stephane Chazelas
  0 siblings, 0 replies; 6+ messages in thread
From: Stephane Chazelas @ 2022-12-11 20:01 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Daniel Shahaf, zsh-workers

2022-12-11 10:52:44 -0800, Bart Schaefer:
[...]
> In any case,
> fileno() doesn't set errno (it's just a data structure deref, would
> only fail by crashing)

Not that it matters much here, but POSIX does say that fileno()
shall  return -1 EBADF if "The stream is not associated with a
file." and may if "The file descriptor underlying stream is not
a valid file descriptor."

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/fileno.html

musl libc has:

int fileno(FILE *f)
{
        FLOCK(f);
        int fd = f->fd;
        FUNLOCK(f);
        if (fd < 0) {
                errno = EBADF;
                return -1;
        }
        return fd;
}

-- 
Stephane


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-12-11 20:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-09 14:10 xtrace output sets ERRNO to 9 (EBADF) Stephane Chazelas
2022-12-10  2:57 ` Bart Schaefer
2022-12-10 11:38   ` Daniel Shahaf
2022-12-11 18:35     ` Stephane Chazelas
2022-12-11 18:52       ` Bart Schaefer
2022-12-11 20:01         ` Stephane Chazelas

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