Skip to content

Commit 852efaf

Browse files
committed
Modify SocketAbstraction.CanWrite to return false when socket is null.
1 parent 939325e commit 852efaf

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

src/Renci.SshNet/Abstractions/SocketAbstraction.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ public static bool CanRead(Socket socket)
2525

2626
}
2727

28+
/// <summary>
29+
/// Returns a value indicating whether the specified <see cref="Socket"/> can be used
30+
/// to send data.
31+
/// </summary>
32+
/// <param name="socket">The <see cref="Socket"/> to check.</param>
33+
/// <returns>
34+
/// <c>true</c> if <paramref name="socket"/> can be written to; otherwise, <c>false</c>.
35+
/// </returns>
2836
public static bool CanWrite(Socket socket)
2937
{
30-
if (socket.Connected)
38+
if (socket != null && socket.Connected)
3139
{
3240
#if FEATURE_SOCKET_POLL
3341
return socket.Poll(-1, SelectMode.SelectWrite);

src/Renci.SshNet/Session.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ internal void WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
834834
/// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
835835
internal void SendMessage(Message message)
836836
{
837-
if (_socket == null || !_socket.CanWrite())
837+
if (!_socket.CanWrite())
838838
throw new SshConnectionException("Client not connected.");
839839

840840
if (_keyExchangeInProgress && !(message is IKeyExchangedAllowed))
@@ -893,7 +893,7 @@ internal void SendMessage(Message message)
893893
}
894894

895895
// increment the packet sequence number only after we're sure the packet has
896-
// been sent; even though it's only used for the MAC, it need to be incremented
896+
// been sent; even though it's only used for the MAC, it needs to be incremented
897897
// for each package sent.
898898
//
899899
// the server will use it to verify the data integrity, and as such the order in
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Net.Sockets;
2+
using Renci.SshNet.Abstractions;
3+
#if SILVERLIGHT
4+
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
5+
#else
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
#endif
8+
9+
namespace Renci.SshNet.Tests.Abstractions
10+
{
11+
[TestClass]
12+
public class SocketAbstraction_CanWrite
13+
{
14+
[TestMethod]
15+
public void ShouldReturnFalseWhenSocketIsNull()
16+
{
17+
const Socket socket = null;
18+
19+
var actual = SocketAbstraction.CanWrite(socket);
20+
21+
Assert.IsFalse(actual);
22+
}
23+
}
24+
}

test/Renci.SshNet.Shared.Tests/Renci.SshNet.Shared.Tests.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<Compile Include="$(MSBuildThisFileDirectory)Abstractions\CryptoAbstraction_GenerateRandom.cs" />
1313
<Compile Include="$(MSBuildThisFileDirectory)Abstractions\DnsAbstraction_GetHostAddresses.cs" />
1414
<Compile Include="$(MSBuildThisFileDirectory)Abstractions\FileSystemAbstraction_EnumerateFiles.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)Abstractions\SocketAbstraction_CanWrite.cs" />
1516
<Compile Include="$(MSBuildThisFileDirectory)Abstractions\ThreadAbstraction_ExecuteThread.cs" />
1617
<Compile Include="$(MSBuildThisFileDirectory)ForwardedPortStatusTest_Started.cs" />
1718
<Compile Include="$(MSBuildThisFileDirectory)ForwardedPortStatusTest_Starting.cs" />

0 commit comments

Comments
 (0)