Avoid rounding issues when checking Timeout values (#1700) (#1712)

* 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

* fix tests

---------

Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
This commit is contained in:
Nikola Milekic
2025-10-08 10:43:13 +02:00
committed by GitHub
co-authored by Robert Hague
parent ebdcb3ea7d
commit c5c6f28d60
5 changed files with 33 additions and 140 deletions
@@ -19,9 +19,9 @@ namespace Renci.SshNet.Common
/// </exception>
public static int AsTimeout(this TimeSpan timeSpan, [CallerArgumentExpression(nameof(timeSpan))] string? paramName = null)
{
var timeoutInMilliseconds = timeSpan.TotalMilliseconds;
return timeoutInMilliseconds is < -1d or > int.MaxValue
? throw new ArgumentOutOfRangeException(paramName, "The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.")
var timeoutInMilliseconds = (long)timeSpan.TotalMilliseconds;
return timeoutInMilliseconds is < -1 or > int.MaxValue
? throw new ArgumentOutOfRangeException(paramName, timeSpan, "The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.")
: (int)timeoutInMilliseconds;
}