Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 19, 2025

When disposing a RarArchive after processing a damaged archive, ArrayPool<byte>.Shared.Return(window) throws ArgumentNullException because the window field may be null.

Changes

  • Added null check in UnpackV1.Unpack.Dispose() before returning the window buffer to the array pool
  • Aligns with existing pattern in UnpackV2017 implementation
// Before
if (!externalWindow)
{
    ArrayPool<byte>.Shared.Return(window);
    window = null;
}

// After  
if (!externalWindow && window is not null)
{
    ArrayPool<byte>.Shared.Return(window);
    window = null;
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Exception when disposing RarArchive</issue_title>
<issue_description>I ran into an exception when disposing RarArchive after processing a damaged archive.

System.ArgumentNullException: Value cannot be null. (Parameter 'array')
   at System.Buffers.SharedArrayPool`1.Return(T[] array, Boolean clearArray)
   at SharpCompress.Compressors.Rar.UnpackV1.Unpack.Dispose() in /_/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs:line 32
   at SharpCompress.Archives.Rar.RarArchive.Dispose() in /_/SharpCompress/Archives/Rar/RarArchive.cs:line 35

I checked in SharpCompress.Compressors.Rar.UnpackV1.Unpack and found:

public override void Dispose()
{
    if (!disposed)
    {
        base.Dispose();
        if (!externalWindow)
        {
            ArrayPool<byte>.Shared.Return(window);
            window = null;
        }
        rarVM.Dispose();
        disposed = true;
    }
}

since "window" was "null" at this point I added a check fo this:

public override void Dispose()
{
    if (!disposed)
    {
        base.Dispose();
        if (!externalWindow && window is not null)
        {
            ArrayPool<byte>.Shared.Return(window);
            window = null;
        }
        rarVM.Dispose();
        disposed = true;
    }
}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix exception when disposing RarArchive Fix ArgumentNullException when disposing RarArchive with damaged archives Nov 19, 2025
Copilot AI requested a review from adamhathcock November 19, 2025 15:22
@adamhathcock adamhathcock marked this pull request as ready for review November 19, 2025 15:27
Copilot AI review requested due to automatic review settings November 19, 2025 15:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an ArgumentNullException that occurs when disposing a RarArchive after processing a damaged archive. The issue arises because the window field may be null when Dispose() is called, but the code attempts to return it to the ArrayPool without checking for null first.

  • Added defensive null check in UnpackV1.Unpack.Dispose() before returning the window buffer to the ArrayPool
  • Aligns with existing null-check patterns used in RarVM.cs, LzOutWindow.cs, and UnpackV2017

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@adamhathcock adamhathcock merged commit 15ca7c9 into master Nov 19, 2025
14 of 15 checks passed
@adamhathcock adamhathcock deleted the copilot/fix-exception-disposing-rararchive branch November 19, 2025 15:48
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.

Exception when disposing RarArchive

2 participants