Skip to content

Commit

Permalink
0.5.2: Fixed premature end of inner stream detection in Decompression…
Browse files Browse the repository at this point in the history
…Stream
  • Loading branch information
oleg-st committed Oct 14, 2021
1 parent 1b93e09 commit 50e8c80
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/ZstdSharp.Test/ZstdNetSteamingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ public void StreamingDecompressionSimpleRead(int readCount)
Assert.True(data.SequenceEqual(buffer));
}

[Fact]
public void StreamingDecompressionTruncatedInput()
{
var dataStream = DataGenerator.GetLargeStream(DataFill.Sequential);

var resultStream = new MemoryStream();
using (var compressionStream = new CompressionStream(resultStream))
dataStream.CopyTo(compressionStream);

// truncate resultStream
var truncatedStream =
new MemoryStream(resultStream.ToArray(), 0, Math.Min(32, (int) resultStream.Length / 3));

var exception = Record.Exception(() =>
{
using var decompressionStream = new DecompressionStream(truncatedStream);
decompressionStream.CopyTo(resultStream);
});
Assert.True(exception is EndOfStreamException);
}

[Fact]
public void StreamingCompressionFlushDataFromInternalBuffers()
{
Expand Down
13 changes: 10 additions & 3 deletions src/ZstdSharp/DecompressionStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ protected override void Dispose(bool disposing)
if (decompressor == null)
return;

if (lastDecompressResult != 0)
throw new EndOfStreamException("Premature end of stream");

decompressor.Dispose();
decompressor = null;
ArrayPool<byte>.Shared.Return(inputBuffer);
Expand All @@ -86,7 +83,12 @@ public int Read(Span<byte> buffer)
{
int bytesRead;
if ((bytesRead = innerStream.Read(inputBuffer, 0, inputBufferSize)) == 0)
{
if (lastDecompressResult != 0)
throw new EndOfStreamException("Premature end of stream");

break;
}

input.size = (nuint) bytesRead;
input.pos = 0;
Expand Down Expand Up @@ -119,7 +121,12 @@ public async ValueTask<int> ReadAsync(Memory<byte> buffer,
int bytesRead;
if ((bytesRead = await innerStream.ReadAsync(inputBuffer, 0, inputBufferSize, cancellationToken)
.ConfigureAwait(false)) == 0)
{
if (lastDecompressResult != 0)
throw new EndOfStreamException("Premature end of stream");

break;
}

input.size = (nuint) bytesRead;
input.pos = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/ZstdSharp/ZstdSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageProjectUrl>https://github.com/oleg-st/ZstdSharp</PackageProjectUrl>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageTags>zstd zstandard port compression</PackageTags>
<Version>0.5.1</Version>
<Version>0.5.2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down

0 comments on commit 50e8c80

Please sign in to comment.