Skip to content

Better handle baseStreams closing themselves unexpectedly #387

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 2 commits into from
Feb 1, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void Fill()
rawLength = 0;
int toRead = rawData.Length;

while (toRead > 0)
while (toRead > 0 && inputStream.CanRead)
{
int count = inputStream.Read(rawData, rawLength, toRead);
if (count <= 0)
Expand Down
34 changes: 33 additions & 1 deletion test/ICSharpCode.SharpZipLib.Tests/GZip/GZipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,41 @@ public void SmallBufferDecompression()


}
}

}

}
/// <summary>
/// Should gracefully handle reading from a stream that becomes unreadable after
/// all of the data has been read.
/// </summary>
/// <remarks>
/// Test for https://github.com/icsharpcode/SharpZipLib/issues/379
/// </remarks>
[Test]
[Category("Zip")]
public void ShouldGracefullyHandleReadingANonReableStream()
{
MemoryStream ms = new SelfClosingStream();
using (var gzos = new GZipOutputStream(ms))
{
gzos.IsStreamOwner = false;

byte[] buf = new byte[100000];
var rnd = new Random();
rnd.NextBytes(buf);

gzos.Write(buf, 0, buf.Length);
}

ms.Seek(0, SeekOrigin.Begin);

using (var gzis = new GZipInputStream(ms))
using (var msRaw = new MemoryStream())
{
gzis.CopyTo(msRaw);
}
}

[Test]
[Category("GZip")]
Expand Down
48 changes: 48 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/TestSupport/Streams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,52 @@ public override int Read(byte[] buffer, int offset, int count)
return base.Read(buffer, offset, count);
}
}

/// <summary>
/// A stream that closes itself when all of its data is read.
/// </summary>
/// <remarks>
/// Useful for testing issues such as https://github.com/icsharpcode/SharpZipLib/issues/379
/// </remarks>
internal class SelfClosingStream : MemoryStream
{
private bool isFullyRead = false;

/// <summary>
/// Initializes a new instance of the <see cref="SelfClosingStream"/> class.
/// </summary>
public SelfClosingStream()
{
}

// <inheritdoc/>
public override int Read(byte[] buffer, int offset, int count)
{
var read = base.Read(buffer, offset, count);

if (read == 0)
{
isFullyRead = true;
Close();
}

return read;
}

/// <summary>
/// CanRead is false if we're closed, or base.CanRead otherwise.
/// </summary>
public override bool CanRead
{
get
{
if (isFullyRead)
{
return false;
}

return base.CanRead;
}
}
}
}