Skip to content

Fix pooled buffer ownership and exception-safe cleanup - #1447

Merged
adamhathcock merged 8 commits into
releasefrom
fix/pooled-buffer-lifetimes
Oct 13, 2026
Merged

adamhathcock merged 8 commits into
releasefrom
fix/pooled-buffer-lifetimes

Conversation

@adamhathcock

Copy link
Copy Markdown
Owner

Summary

  • Prevent repeated disposal of CrcCheckStream from returning the same pooled array twice, and reject writes after disposal.
  • Rent replacement buffers before returning existing buffers in the LZMA window, match finder, literal encoder, and sync/async RAR filter paths.
  • Release LZMA2 encoder buffers even when flushing or writing the end marker fails.
  • Add 14 deterministic regression cases covering disposal, allocation failure, and tar buffer lifetime, including cancellation and truncated headers.
  • Retain the tar lifetime fix already present on release; add an internal pool-injection hook for its regression tests.
  • Document CRC-checking stream disposal behavior.

Validation

  • Full net10.0 test suite passed: 2,308 tests.
  • Library built successfully for net48, netstandard2.0, netstandard2.1, net6.0, net8.0, and net10.0 with zero warnings or errors.
  • dotnet csharpier format ., dotnet csharpier check ., and git diff --check completed successfully.

Based on the latest origin/release; follows up on b924008.

Copilot AI balanced review requested due to automatic review settings October 13, 2026 08:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

LZMA2 construction can leak a rented buffer, and writes remain possible after that buffer is returned.

1 open finding
What changed in this PR

Improves pooled-buffer ownership and exception-safe cleanup across LZMA, RAR, and Tar paths.

Changes:

  • Makes buffer replacement and disposal safer.
  • Adds injectable pools and 14 regression cases.
  • Documents CrcCheckStream disposal behavior.
File Description
tests/​SharpCompress.Test/​Streams/​BufferPoolLifetimeTests.cs Adds buffer-lifetime regressions.
src/​SharpCompress/​Compressors/​Rar/​UnpackV1/​Unpack.cs Safely replaces sync filter buffers.
src/​SharpCompress/​Compressors/​Rar/​UnpackV1/​Unpack.Async.cs Safely replaces async filter buffers.
src/​SharpCompress/​Compressors/​LZMA/​Utilities/​CrcCheckStream.cs Makes disposal idempotent and rejects writes afterward.
src/​SharpCompress/​Compressors/​LZMA/​LzmaEncoder.cs Safely replaces literal models.
src/​SharpCompress/​Compressors/​LZMA/​Lzma2EncoderStream.cs Releases encoder buffers after output failures.
src/​SharpCompress/​Compressors/​LZMA/​LZ/​LzOutWindow.cs Adds injectable pooling and safe replacement.
src/​SharpCompress/​Compressors/​LZMA/​LZ/​LzOutWindow.Async.cs Uses the injected pool during async cleanup.
src/​SharpCompress/​Compressors/​LZMA/​LZ/​LzBinTree.cs Safely replaces match-finder buffers.
src/​SharpCompress/​Common/​Tar/​Headers/​TarHeader.Async.cs Adds pool injection for lifetime tests.
docs/​API.md Documents CRC stream disposal semantics.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@adamhathcock
adamhathcock merged commit 9aa94c4 into release Oct 13, 2026
3 checks passed
@adamhathcock
adamhathcock deleted the fix/pooled-buffer-lifetimes branch October 13, 2026 09:55
This was referenced Nov 16, 2026
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 this pull request may close these issues.

2 participants