Skip to content

Fix ArrayPool corruption from double-disposal in BufferedSubStream#1161

Merged
adamhathcock merged 4 commits intoadam/check-if-seekfrom
copilot/sub-pr-1160
Jan 26, 2026
Merged

Fix ArrayPool corruption from double-disposal in BufferedSubStream#1161
adamhathcock merged 4 commits intoadam/check-if-seekfrom
copilot/sub-pr-1160

Conversation

Copy link
Contributor

Copilot AI commented Jan 26, 2026

BufferedSubStream.Dispose returns a rented array to ArrayPool but lacks guard against multiple disposals. Since _cache was readonly, it couldn't be nulled after return, causing the same array to be returned twice on subsequent Dispose calls—corrupting the pool.

Changes

  • Added disposal guard: _isDisposed flag prevents re-execution of disposal logic
  • Made _cache nullable: Changed from readonly byte[] to byte[]? to enable nulling after pool return
  • Added disposal checks: RefillCache methods throw ObjectDisposedException if called post-disposal
  • Fixed condition order: Check CanSeek before accessing Position to avoid exceptions on non-seekable streams

Pattern

Follows the same disposal pattern as base class SharpCompressStream:

protected override void Dispose(bool disposing)
{
    if (_isDisposed) return;
    _isDisposed = true;
    
    if (disposing && _cache is not null)
    {
        ArrayPool<byte>.Shared.Return(_cache);
        _cache = null;
    }
    base.Dispose(disposing);
}

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Address feedback on seeking check functionality in PR #1160 Fix ArrayPool corruption from double-disposal in BufferedSubStream Jan 26, 2026
Copilot AI requested a review from adamhathcock January 26, 2026 12:15
@adamhathcock adamhathcock marked this pull request as ready for review January 26, 2026 12:15
@adamhathcock adamhathcock merged commit 7dcc13c into adam/check-if-seek Jan 26, 2026
@adamhathcock adamhathcock deleted the copilot/sub-pr-1160 branch January 26, 2026 12:15
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