The Bison 3.0.2 test suite failed in one test with a strange error saying "_open_osfhandle failed: Too many open files". Looking into this, I found out that dupfd sets errno to EMFILE for no good reason: _open_osfhandle itself sets errno, including to EMFILE if needed, so there's no need to override that. In this case, errno was EBADF, and the code that called dup_noinherit was perfectly capable of dealing with the consequences of that.
The patch to fix that is below. While at that, I also took care of TODO item regarding error codes from DuplicateHandle. 2014-10-07 Eli Zaretskii <e...@gnu.org> Fix error reporting by dupfd. * lib/fcntl.c (dupfd) [_WIN32]: Don't overwrite the value of errno set by _open_osfhandle by EMFILE. Convert errors returned by DuplicateHandle to corresponding errno values. --- lib/fcntl.c~0 2013-08-02 18:03:13 +0300 +++ lib/fcntl.c 2014-10-07 08:42:24 +0300 @@ -89,8 +89,25 @@ dupfd (int oldfd, int newfd, int flags) inherit, /* InheritHandle */ DUPLICATE_SAME_ACCESS)) /* Options */ { - /* TODO: Translate GetLastError () into errno. */ - errno = EMFILE; + switch (GetLastError ()) + { + case ERROR_TOO_MANY_OPEN_FILES: + errno = EMFILE; + break; + case ERROR_INVALID_HANDLE: + case ERROR_INVALID_TARGET_HANDLE: + case ERROR_DIRECT_ACCESS_HANDLE: + errno = EBADF; + break; + case ERROR_INVALID_PARAMETER: + case ERROR_INVALID_FUNCTION: + case ERROR_INVALID_ACCESS: + errno = EINVAL; + break; + default: + errno = EACCES; + break; + } result = -1; break; } @@ -98,7 +115,6 @@ dupfd (int oldfd, int newfd, int flags) if (duplicated_fd < 0) { CloseHandle (new_handle); - errno = EMFILE; result = -1; break; }