Skip to content

Commit

Permalink
Ensure compression buffers do not overlap (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
brantburnett authored Dec 23, 2024
1 parent 9067cab commit a7de589
Show file tree
Hide file tree
Showing 4 changed files with 15 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
2 changes: 2 additions & 0 deletions Snappier/Snappy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static int GetMaxCompressedLength(int inputLength) =>
/// <param name="output">Buffer to receive the compressed data.</param>
/// <returns>Number of bytes written to <paramref name="output"/>.</returns>
/// <exception cref="ArgumentException">Output buffer is too small.</exception>
/// <exception cref="InvalidOperationException">Input and output spans must not overlap.</exception>
/// <remarks>
/// The output buffer must be large enough to contain the compressed output.
/// </remarks>
Expand All @@ -51,6 +52,7 @@ public static int Compress(ReadOnlySpan<byte> input, Span<byte> output)
/// <param name="input">Data to compress.</param>
/// <param name="output">Buffer to receive the compressed data.</param>
/// <param name="bytesWritten">Number of bytes written to the <paramref name="output"/>.</param>
/// <exception cref="InvalidOperationException">Input and output spans must not overlap.</exception>
/// <returns><c>true</c> if the compression was successful, <c>false</c> if the output buffer is too small.</returns>
public static bool TryCompress(ReadOnlySpan<byte> input, Span<byte> output, out int bytesWritten)
{
Expand Down

0 comments on commit a7de589

Please sign in to comment.