]> Savannah Git Hosting - gnulib.git/commitdiff
openat: omit unnecessary fd test
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 18 Nov 2024 16:13:26 +0000 (08:13 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 20 Nov 2024 19:23:33 +0000 (11:23 -0800)
* lib/openat.c (openat_permissive): Close fd regardless
of whether it’s STDERR_FILENO.  This saves a bit of code space
and there’s no point to making this unlikely path faster.
Also change a couple of != 0 to < 0 for clarity
when -1 is the only option.

ChangeLog
lib/openat.c

index 11d0a0f67992014bbad6c7ce352b8a838174cf2c..567370aa3e2697cebb17574bc6e635249691b7f3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2024-11-20  Paul Eggert  <eggert@cs.ucla.edu>
 
+       openat: omit unnecessary fd test
+       * lib/openat.c (openat_permissive): Close fd regardless
+       of whether it’s STDERR_FILENO.  This saves a bit of code space
+       and there’s no point to making this unlikely path faster.
+       Also change a couple of != 0 to < 0 for clarity
+       when -1 is the only option.
+
        openat: use C99 decls after stmts
        * lib/openat.c (rpl_openat, openat_permissive):
        Refactor to put decls closer to where they’re used.
index 9c8180c01347272a365be9b49f96c5856b023aec..0e8e819408becfc1a65656dae3f4265cfd5ba580 100644 (file)
@@ -271,18 +271,17 @@ openat_permissive (int fd, char const *file, int flags, mode_t mode,
   int err = fchdir (fd);
   int saved_errno = errno;
 
-  if (! err)
+  if (0 <= err)
     {
       err = open (file, flags, mode);
       saved_errno = errno;
-      if (!save_failed && restore_cwd (&saved_cwd) != 0)
+      if (!save_failed && restore_cwd (&saved_cwd) < 0)
         {
           if (! cwd_errno)
             {
               /* Don't write a message to just-created fd 2.  */
               saved_errno = errno;
-              if (err == STDERR_FILENO)
-                close (err);
+              close (err);
               openat_restore_fail (saved_errno);
             }
           *cwd_errno = errno;