Skip to content

Commit

Permalink
Merge pull request #796 from tristanlabelle/clamp-pipe-WriteFile
Browse files Browse the repository at this point in the history
Prevent infinite attempts to write to Win32 pipes
  • Loading branch information
compnerd authored Jun 15, 2023
2 parents 10d9f92 + 0c38954 commit 9566a13
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2510,6 +2510,23 @@ _dispatch_operation_perform(dispatch_operation_t op)
}
bSuccess = TRUE;
} else if (GetFileType(hFile) == FILE_TYPE_PIPE) {
// WriteFile with more bytes than are available in the
// buffer of a NOWAIT pipe will immediately return 0,
// so clamp our requested write length to make progress.
IO_STATUS_BLOCK iosb;
FILE_PIPE_LOCAL_INFORMATION fpli;
NTSTATUS status = _dispatch_NtQueryInformationFile(hFile,
&iosb, &fpli, sizeof(fpli), FilePipeLocalInformation);
if (NT_SUCCESS(status)) {
// WriteQuotaAvailable is unreliable in the presence
// of a blocking reader, when it can return zero, so only
// account for it otherwise
if (fpli.WriteQuotaAvailable > 0) {
len = MIN(len, fpli.WriteQuotaAvailable);
}
len = MIN(len, fpli.OutboundQuota);
}

OVERLAPPED ovlOverlapped = {};
bSuccess = WriteFile(hFile, buf, (DWORD)len,
(LPDWORD)&processed, &ovlOverlapped);
Expand Down

0 comments on commit 9566a13

Please sign in to comment.