+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
int dupfd;
tempfd = open ("NUL", O_RDONLY);
- if (tempfd == -1)
- return -1;
+ if (tempfd < 0)
+ return tempfd;
if (tempfd >= 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;
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);
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;