Skip to content

Fix PipeStream.Read returning 0 prematurely #924

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

Closed
wants to merge 4 commits into from
Closed
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
17 changes: 14 additions & 3 deletions src/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Read()

var readBuffer = new byte[2];
var bytesRead = target.Read(readBuffer, 0, readBuffer.Length);
Assert.AreEqual(2, bytesRead);
Assert.AreEqual(readBuffer.Length, bytesRead);
Assert.AreEqual(0x0a, readBuffer[0]);
Assert.AreEqual(0x0d, readBuffer[1]);

Expand All @@ -76,9 +76,20 @@ public void Read()
});
writeToStreamThread.Start();

target.BlockLastReadBuffer = true;
readBuffer = new byte[2];
bytesRead = target.Read(readBuffer, 0, readBuffer.Length);
Assert.AreEqual(2, bytesRead);
int bufferOffset = 0;
while (bufferOffset < readBuffer.Length)
{
bytesRead = target.Read(readBuffer, bufferOffset, readBuffer.Length - bufferOffset);
if (bytesRead == 0)
{
Assert.Fail("PipeStream.Read returned 0");
}

bufferOffset += bytesRead;
}
Assert.AreEqual(readBuffer.Length, bufferOffset);
Assert.AreEqual(0x09, readBuffer[0]);
Assert.AreEqual(0x05, readBuffer[1]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class PipeStream_Close_BlockingRead : TripleATestBase
protected override void Arrange()
{
_pipeStream = new PipeStream();
_pipeStream.BlockLastReadBuffer = true;

_pipeStream.WriteByte(10);
_pipeStream.WriteByte(13);
Expand Down
16 changes: 2 additions & 14 deletions src/Renci.SshNet/Common/PipeStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ public class PipeStream : Stream
/// <remarks>Possible more effecient ways to accomplish this.</remarks>
private readonly Queue<byte> _buffer = new Queue<byte>();

/// <summary>
/// Indicates that the input stream has been flushed and that
/// all remaining data should be written to the output stream.
/// </summary>
private bool _isFlushed;

/// <summary>
/// Maximum number of bytes to store in the buffer.
/// </summary>
Expand Down Expand Up @@ -131,7 +125,6 @@ public override void Flush()
if (_isDisposed)
throw CreateObjectDisposedException();

_isFlushed = true;
lock (_buffer)
{
// unblock read hereby allowing buffer to be partially filled
Expand Down Expand Up @@ -180,8 +173,6 @@ public override void SetLength(long value)
///<exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
public override int Read(byte[] buffer, int offset, int count)
{
if (offset != 0)
throw new NotSupportedException("Offsets with value of non-zero are not supported");
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset + count > buffer.Length)
Expand Down Expand Up @@ -213,7 +204,7 @@ public override int Read(byte[] buffer, int offset, int count)
// fill the read buffer
for (; readLength < count && _buffer.Count > 0; readLength++)
{
buffer[readLength] = _buffer.Dequeue();
buffer[offset + readLength] = _buffer.Dequeue();
}

Monitor.Pulse(_buffer);
Expand All @@ -229,8 +220,7 @@ public override int Read(byte[] buffer, int offset, int count)
/// <returns><c>True</c> if data available; otherwise<c>false</c>.</returns>
private bool ReadAvailable(int count)
{
var length = Length;
return (_isFlushed || length >= count) && (length >= (count + 1) || !BlockLastReadBuffer);
return BlockLastReadBuffer ? Length >= count : Length > 0;
}

///<summary>
Expand Down Expand Up @@ -264,8 +254,6 @@ public override void Write(byte[] buffer, int offset, int count)
while (Length >= _maxBufferLength)
Monitor.Wait(_buffer);

_isFlushed = false; // if it were flushed before, it soon will not be.

// queue up the buffer data
for (var i = offset; i < offset + count; i++)
{
Expand Down