Skip to content

Commit 79ad872

Browse files
committed
add ThrowHelper.ThrowIfNegative
1 parent c1c9744 commit 79ad872

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

src/Renci.SshNet/Common/PacketDump.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ public static string Create(List<byte> data, int indentLevel)
1515
public static string Create(byte[] data, int indentLevel)
1616
{
1717
ThrowHelper.ThrowIfNull(data);
18-
19-
if (indentLevel < 0)
20-
{
21-
throw new ArgumentOutOfRangeException(nameof(indentLevel), "Cannot be less than zero.");
22-
}
18+
ThrowHelper.ThrowIfNegative(indentLevel);
2319

2420
const int lineWidth = 16;
2521

src/Renci.SshNet/Common/ThrowHelper.cs

+17-9
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,39 @@ static void Throw(string? argument, string? paramName)
8787
public static void ValidateBufferArguments(byte[] buffer, int offset, int count)
8888
{
8989
ThrowIfNull(buffer);
90+
ThrowIfNegative(offset);
9091

91-
if (offset < 0)
92+
if ((uint)count > buffer.Length - offset)
9293
{
9394
Throw();
9495

9596
[DoesNotReturn]
9697
static void Throw()
9798
{
98-
throw new ArgumentOutOfRangeException(nameof(offset), "Non-negative number required.");
99+
throw new ArgumentOutOfRangeException(
100+
nameof(count),
101+
"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
99102
}
100103
}
104+
}
105+
#endif
101106

102-
if ((uint)count > buffer.Length - offset)
107+
public static void ThrowIfNegative(long value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
108+
{
109+
#if NET
110+
ArgumentOutOfRangeException.ThrowIfNegative(value, paramName);
111+
#else
112+
if (value < 0)
103113
{
104-
Throw();
114+
Throw(value, paramName);
105115

106116
[DoesNotReturn]
107-
static void Throw()
117+
static void Throw(long value, string? paramName)
108118
{
109-
throw new ArgumentOutOfRangeException(
110-
nameof(count),
111-
"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
119+
throw new ArgumentOutOfRangeException(paramName, value, "Value must be non-negative.");
112120
}
113121
}
114-
}
115122
#endif
123+
}
116124
}
117125
}

src/Renci.SshNet/Sftp/SftpFileStream.cs

+1-8
Original file line numberDiff line numberDiff line change
@@ -922,14 +922,7 @@ public override long Seek(long offset, SeekOrigin origin)
922922
/// </remarks>
923923
public override void SetLength(long value)
924924
{
925-
#if NET
926-
ArgumentOutOfRangeException.ThrowIfNegative(value);
927-
#else
928-
if (value < 0)
929-
{
930-
throw new ArgumentOutOfRangeException(nameof(value));
931-
}
932-
#endif
925+
ThrowHelper.ThrowIfNegative(value);
933926

934927
// Lock down the file stream while we do this.
935928
lock (_lock)

test/Renci.SshNet.Tests/Classes/Common/PacketDumpTest.cs

-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public void Create_ByteArrayAndIndentLevel_IndentLevelLessThanZero()
4141
{
4242
Assert.IsNull(ex.InnerException);
4343

44-
ArgumentExceptionAssert.MessageEquals("Cannot be less than zero.", ex);
45-
4644
Assert.AreEqual("indentLevel", ex.ParamName);
4745
}
4846
}

0 commit comments

Comments
 (0)