Skip to content

Fix ShellStream when receiving data larger than buffer length #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Renci.SshNet/ShellStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void AssertValid()
Debug.Assert(Monitor.IsEntered(_sync), $"Should be in lock on {nameof(_sync)}");
Debug.Assert(_readHead >= 0, $"{nameof(_readHead)} should be non-negative but is {_readHead}");
Debug.Assert(_readTail >= 0, $"{nameof(_readTail)} should be non-negative but is {_readTail}");
Debug.Assert(_readHead < _readBuffer.Length || _readBuffer.Length == 0, $"{nameof(_readHead)} should be < {nameof(_readBuffer)}.Length but is {_readHead}");
Debug.Assert(_readHead <= _readBuffer.Length, $"{nameof(_readHead)} should be <= {nameof(_readBuffer)}.Length but is {_readHead}");
Debug.Assert(_readTail <= _readBuffer.Length, $"{nameof(_readTail)} should be <= {nameof(_readBuffer)}.Length but is {_readTail}");
Debug.Assert(_readHead <= _readTail, $"Should have {nameof(_readHead)} <= {nameof(_readTail)} but have {_readHead} <= {_readTail}");
}
Expand Down Expand Up @@ -938,7 +938,7 @@ private void Channel_DataReceived(object? sender, ChannelDataEventArgs e)
else
{
// Otherwise, we're gonna need a bigger buffer.
var newBuffer = new byte[_readBuffer.Length * 2];
var newBuffer = new byte[Math.Max(newLength, _readBuffer.Length * 2)];
Buffer.BlockCopy(_readBuffer, _readHead, newBuffer, 0, _readTail - _readHead);
_readBuffer = newBuffer;
}
Expand Down
14 changes: 14 additions & 0 deletions test/Renci.SshNet.Tests/Classes/ShellStreamTest_ReadExpect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using Moq;

using Renci.SshNet.Abstractions;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;

Expand Down Expand Up @@ -70,6 +71,19 @@ public void Read_Bytes()
CollectionAssert.AreEqual(Encoding.UTF8.GetBytes("orld!llo W\0\0"), buffer);
}

[TestMethod]
public void Channel_DataReceived_MoreThanBufferSize()
{
// Test buffer resizing
byte[] expectedData = CryptoAbstraction.GenerateRandom(BufferSize * 3);
_channelSessionStub.Receive(expectedData);

byte[] actualData = new byte[expectedData.Length + 1];

Assert.AreEqual(expectedData.Length, _shellStream.Read(actualData, 0, actualData.Length));
CollectionAssert.AreEqual(expectedData, actualData.Take(expectedData.Length));
}

[DataTestMethod]
[DataRow("\r\n")]
[DataRow("\r")]
Expand Down