Skip to content

Commit b161847

Browse files
committed
[1.6] Fix: FileDescriptor.duplicate(as:) returns an invalid file descriptor on Windows
FileDescriptor.duplicate(as:) is backed by dup2 on Unix, and _dup2 on Windows. On Unix, dup2 returns its second argument. On Windows, _dup2 instead returns 0 on success and -1 on error. This results in the newly returned FileDescriptor object always containing a '0' file descriptor rather than the newly created file descriptor, in violation of the documented behavior. Account for the platform difference in the syscall wrapper to fix this. Closes #192 (cherry picked from commit b2711a8)
1 parent 890830f commit b161847

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Sources/System/Internals/WindowsSyscallAdapters.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ internal func dup(_ fd: Int32) -> Int32 {
135135

136136
@inline(__always)
137137
internal func dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
138-
_dup2(fd, fd2)
138+
// _dup2 returns 0 to indicate success.
139+
if _dup2(fd, fd2) == 0 {
140+
return fd2
141+
}
142+
return -1
139143
}
140144

141145
@inline(__always)

0 commit comments

Comments
 (0)