Mercurial > gnulib
changeset 34501:972dc91169f0
opendir-safer.c: don't clobber errno; don't close negative FD
* lib/opendir-safer.c (opendir_safer):
[HAVE_FDOPENDIR || GNULIB_FDOPENDIR]: Don't close a negative
file descriptor, and more importantly, don't clobber the
offending errno value with EINVAL. Before, upon failure
of dup_safer, we would pass the negative file descriptor to
fdopendir, which would clobber errno.
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Tue, 24 May 2011 13:44:41 +0200 |
parents | 77298decda83 |
children | 9a319c741d7b |
files | ChangeLog lib/opendir-safer.c |
diffstat | 2 files changed, 22 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Mon May 23 16:38:11 2011 -0700 +++ b/ChangeLog Tue May 24 13:44:41 2011 +0200 @@ -1,3 +1,13 @@ +2011-05-24 Jim Meyering <meyering@redhat.com> + + opendir-safer.c: don't clobber errno; don't close negative FD + * lib/opendir-safer.c (opendir_safer): + [HAVE_FDOPENDIR || GNULIB_FDOPENDIR]: Don't close a negative + file descriptor, and more importantly, don't clobber the + offending errno value with EINVAL. Before, upon failure + of dup_safer, we would pass the negative file descriptor to + fdopendir, which would clobber errno. + 2011-05-23 Bruno Haible <bruno@clisp.org> idcache: Fix module description.
--- a/lib/opendir-safer.c Mon May 23 16:38:11 2011 -0700 +++ b/lib/opendir-safer.c Tue May 24 13:44:41 2011 +0200 @@ -50,10 +50,18 @@ int e; #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR int f = dup_safer (fd); - newdp = fdopendir (f); - e = errno; - if (! newdp) - close (f); + if (f < 0) + { + e = errno; + newdp = NULL; + } + else + { + newdp = fdopendir (f); + e = errno; + if (! newdp) + close (f); + } #else /* !FDOPENDIR */ newdp = opendir_safer (name); e = errno;