Skip to content

Commit

Permalink
Ensure compression buffers do not overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
brantburnett committed Dec 22, 2024
1 parent 9067cab commit 8f0dd2f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Snappier.Tests/SnappyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ public void CompressAndDecompressString(string str)
Assert.Equal(input, output);
}

[Fact]
public void Compress_OverlappingBuffers_InvalidOperationException()
{
var input = new byte[1024];

Assert.Throws<InvalidOperationException>(() => Snappy.Compress(input, input.AsSpan(input.Length - 1)));
}

[Fact]
public void BadData_InsufficentOutputBuffer_ThrowsArgumentException()
{
Expand Down
4 changes: 4 additions & 0 deletions Snappier/Internal/SnappyCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public bool TryCompress(ReadOnlySpan<byte> input, Span<byte> output, out int byt
{
ThrowHelper.ThrowObjectDisposedException(nameof(SnappyCompressor));
}
if (input.Overlaps(output))
{
ThrowHelper.ThrowInvalidOperationException("Input and output spans must not overlap");
}

_workingMemory.EnsureCapacity(input.Length);

Expand Down
2 changes: 1 addition & 1 deletion Snappier/Snappier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from the official C++ implementation, with the addition of support for the framed stream format.

By avoiding P/Invoke, Snappier is fully cross-platform and works on both Linux and Windows and against any CPU supported
by .NET Core. However, Snappier performs best in .NET Core 3.0 and later on little-endian x86/64 processors with the
by .NET. However, Snappier performs best in .NET 6 and later on little-endian x86/64 processors with the
help of System.Runtime.Instrinsics.
</Description>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
Expand Down

0 comments on commit 8f0dd2f

Please sign in to comment.