Skip to content

Commit

Permalink
Clamp long timeouts on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
sirhcel committed Aug 2, 2024
1 parent 129b4d1 commit a192b66
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/windows/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,15 @@ impl COMPort {
}

fn timeout_constant(duration: Duration) -> DWORD {
duration.as_millis() as DWORD
let milliseconds = duration.as_millis();
// In the way we are setting up COMMTIMEOUTS, a timeout_constant of MAXDWORD gets rejected.
// Let's clamp the timeout constant for values of MAXDWORD and above. See remarks at
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-commtimeouts.
//
// This effectively throws away accuracy for really long timeouts but at least preserves a
// long-ish timeout. But just casting to DWORD would result in presumably unexpected short
// and non-monotonic timeouts from cutting off the higher bits.
u128::min(milliseconds, MAXDWORD as u128 - 1) as DWORD
}
}

Expand Down

0 comments on commit a192b66

Please sign in to comment.