]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
dup2: help the compiler a bit
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 2 Dec 2025 18:42:52 +0000 (10:42 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 2 Dec 2025 18:43:07 +0000 (10:43 -0800)
* lib/dup2.c (klibc_dup2dirfd, klibc_dup2, rpl_dup2): Help the
compiler leave a register alone, and/or test just the sign bit.

ChangeLog
lib/dup2.c

index 8ea5a65cf989c28457c458461def0fa61ea0ddd3..22bb59ee1d9587c45c1722bad4e8c15673c56481 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-12-02  Paul Eggert  <eggert@cs.ucla.edu>
+
+       dup2: help the compiler a bit
+       * lib/dup2.c (klibc_dup2dirfd, klibc_dup2, rpl_dup2): Help the
+       compiler leave a register alone, and/or test just the sign bit.
+
 2025-12-02  KO Myung-Hun  <komh78@gmail.com>
 
        dup2, fcntl: Fix the race condition on OS/2 kLIBC
index 3468fbcb6e8a3600a5032983bffed5f9b7305b8c..434e78c2bc67a00b2d09935244b9eefbf7886ac2 100644 (file)
@@ -114,8 +114,8 @@ klibc_dup2dirfd (int fd, int desired_fd)
   int dupfd;
 
   tempfd = open ("NUL", O_RDONLY);
-  if (tempfd == -1)
-    return -1;
+  if (tempfd < 0)
+    return tempfd;
 
   if (tempfd >= desired_fd)
     {
@@ -130,8 +130,8 @@ klibc_dup2dirfd (int fd, int desired_fd)
           close (desired_fd);
 
           dupfd = open (path, O_RDONLY);
-          if (dupfd == -1)
-            return -1;
+          if (dupfd < 0)
+            return dupfd;
 
           if (dupfd == desired_fd)
             return dupfd;
@@ -164,7 +164,7 @@ klibc_dup2 (int fd, int desired_fd)
   struct stat sbuf;
 
   dupfd = dup2 (fd, desired_fd);
-  if (dupfd == -1 && errno == ENOTSUP \
+  if (dupfd < 0 && errno == ENOTSUP \
       && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
     return klibc_dup2dirfd (fd, desired_fd);
 
@@ -197,10 +197,11 @@ rpl_dup2 (int fd, int desired_fd)
   result = dup2 (fd, desired_fd);
 
   /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x.  */
-  if (result == -1 && errno == EMFILE)
+  if (result < 0 && errno == EMFILE)
     errno = EBADF;
+
 #if REPLACE_FCHDIR
-  if (fd != desired_fd && result != -1)
+  if (! (result < 0 || fd == desired_fd))
     result = _gl_register_dup (fd, result);
 #endif
   return result;