Skip to content
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

BrotliCompressor.CompressCore rare case #254

Closed
kimk-s opened this issue Mar 17, 2024 · 0 comments · Fixed by #263
Closed

BrotliCompressor.CompressCore rare case #254

kimk-s opened this issue Mar 17, 2024 · 0 comments · Fixed by #263

Comments

@kimk-s
Copy link

kimk-s commented Mar 17, 2024

Hello,
I'm not sure exactly what's causing this, but the code below causes an infinite loop.

https://github.com/Cysharp/MemoryPack/blob/main/src/MemoryPack.Core/Compression/BrotliCompressor.cs#L263
while (lastResult == OperationStatus.DestinationTooSmall)

Case1

var buffer = new ArrayBufferWriter<byte>();
using var state = MemoryPackWriterOptionalStatePool.Rent(null);
var writer = new MemoryPackWriter<ArrayBufferWriter<byte>>(ref buffer, state);

var compressor = new BrotliCompressor(CompressionLevel.Fastest, 22);
var coWriter = new MemoryPackWriter<BrotliCompressor>(ref compressor, writer.OptionalState);    // no write to coWriter

compressor.CopyTo(ref writer);

Case2
In this, an infinite loop occurs. (BrotliEncoderOperation.Finish)
https://github.com/Cysharp/MemoryPack/blob/main/src/MemoryPack.Core/Compression/BrotliCompressor.cs#L217

using var state = MemoryPackWriterOptionalStatePool.Rent(null);
var buffer = new ArrayBufferWriter<byte>();    // ArrayBufferWriter.DefaultInitialBufferSize: 256
var writer = new MemoryPackWriter<ArrayBufferWriter<byte>>(ref buffer, state);

var compressor = new BrotliCompressor(CompressionLevel.Fastest, 22);
var coWriter = new MemoryPackWriter<BrotliCompressor>(ref compressor, writer.OptionalState);

var bytes = new byte[248];    // 'bytes' length is 248.
Random.Shared.NextBytes(bytes);
coWriter.WriteUnmanagedSpan(bytes.AsSpan());
coWriter.Flush();

compressor.CopyTo(ref writer);

Case3
In this, an infinite loop occurs. (not final block)
https://github.com/Cysharp/MemoryPack/blob/main/src/MemoryPack.Core/Compression/BrotliCompressor.cs#L212

using var state = MemoryPackWriterOptionalStatePool.Rent(null);
var buffer = new ArrayBufferWriter<byte>();    // ArrayBufferWriter.DefaultInitialBufferSize is 256
var writer = new MemoryPackWriter<ArrayBufferWriter<byte>>(ref buffer, state);

var compressor = new BrotliCompressor(CompressionLevel.Fastest, 22);
var coWriter = new MemoryPackWriter<BrotliCompressor>(ref compressor, writer.OptionalState);

var bytes = new byte[249];    // 'bytes' length is 249.
Random.Shared.NextBytes(bytes);
coWriter.WriteUnmanagedSpan(bytes.AsSpan());
coWriter.Flush();

compressor.CopyTo(ref writer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant