Skip to content

Commit f72414c

Browse files
committed
Avoid rounding issues when checking Timeout values (#1700)
AsTimeout is called from the SshCommand constructor with Timeout.InfiniteTimeSpan. In this scenario the range check should never fail, but unfortunately it does in certain scenarios, due to a runtime or compiler bug (as soon as optimizations are turned off the issue miraculously disappears). Closes #1700
1 parent ebdcb3e commit f72414c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/Renci.SshNet/Common/TimeSpanExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ internal static class TimeSpanExtensions
1919
/// </exception>
2020
public static int AsTimeout(this TimeSpan timeSpan, [CallerArgumentExpression(nameof(timeSpan))] string? paramName = null)
2121
{
22-
var timeoutInMilliseconds = timeSpan.TotalMilliseconds;
23-
return timeoutInMilliseconds is < -1d or > int.MaxValue
24-
? throw new ArgumentOutOfRangeException(paramName, "The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.")
22+
var timeoutInMilliseconds = (long)timeSpan.TotalMilliseconds;
23+
return timeoutInMilliseconds is < -1 or > int.MaxValue
24+
? throw new ArgumentOutOfRangeException(paramName, timeSpan, "The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.")
2525
: (int)timeoutInMilliseconds;
2626
}
2727

0 commit comments

Comments
 (0)