]> Savannah Git Hosting - gnulib.git/commitdiff
openat: don’t close (-1)
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 21 Nov 2024 01:51:05 +0000 (17:51 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 21 Nov 2024 01:51:54 +0000 (17:51 -0800)
* lib/openat.c (openat_permissive): Don’t close (-1), as some
debugging systems dislike that.  Clarify errno handling a bit.

ChangeLog
lib/openat.c

index e03ab61ceb4247297d0f1b1f65915dd51de36aa3..9d412f9cbc7984bf2e5a5924d77236a6d693ea16 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-11-20  Paul Eggert  <eggert@cs.ucla.edu>
 
+       openat: don’t close (-1)
+       * lib/openat.c (openat_permissive): Don’t close (-1), as some
+       debugging systems dislike that.  Clarify errno handling a bit.
+
        tests: omit MEMCHR etc as no longer needed
        * tests/test-memchr.c (MEMCHR):
        * tests/test-memchr2.c (MEMCHR2):
index 0e8e819408becfc1a65656dae3f4265cfd5ba580..609b03d627aa06347f7a4f3a7afca1553b2a82bf 100644 (file)
@@ -277,14 +277,16 @@ openat_permissive (int fd, char const *file, int flags, mode_t mode,
       saved_errno = errno;
       if (!save_failed && restore_cwd (&saved_cwd) < 0)
         {
+          int restore_cwd_errno = errno;
           if (! cwd_errno)
             {
-              /* Don't write a message to just-created fd 2.  */
-              saved_errno = errno;
-              close (err);
-              openat_restore_fail (saved_errno);
+              /* Do not leak ERR.  This also stops openat_restore_fail
+                 from messing up if ERR happens to equal STDERR_FILENO.  */
+              if (0 <= err)
+                close (err);
+              openat_restore_fail (restore_cwd_errno);
             }
-          *cwd_errno = errno;
+          *cwd_errno = restore_cwd_errno;
         }
     }