[PR #1025] [MERGED] Fix ArgumentNullException when disposing RarArchive with damaged archives #1448

Closed
opened 2026-01-29 22:20:37 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/1025
Author: @Copilot
Created: 11/19/2025
Status: Merged
Merged: 11/19/2025
Merged by: @adamhathcock

Base: masterHead: copilot/fix-exception-disposing-rararchive


📝 Commits (2)

  • 72cf77b Initial plan
  • 31f81f3 Fix null reference exception in UnpackV1.Unpack.Dispose()

📊 Changes

1 file changed (+1 additions, -1 deletions)

View changed files

📝 src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs (+1 -1)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/adamhathcock/sharpcompress/pull/1025 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 11/19/2025 **Status:** ✅ Merged **Merged:** 11/19/2025 **Merged by:** [@adamhathcock](https://github.com/adamhathcock) **Base:** `master` ← **Head:** `copilot/fix-exception-disposing-rararchive` --- ### 📝 Commits (2) - [`72cf77b`](https://github.com/adamhathcock/sharpcompress/commit/72cf77b7c7a5ab618bff198640fb9c8ebb2be6a6) Initial plan - [`31f81f3`](https://github.com/adamhathcock/sharpcompress/commit/31f81f38affecb3f90659eb9d34df68a11a62035) Fix null reference exception in UnpackV1.Unpack.Dispose() ### 📊 Changes **1 file changed** (+1 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs` (+1 -1) </details> ### 📄 Description 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 ```csharp // Before if (!externalWindow) { ArrayPool<byte>.Shared.Return(window); window = null; } // After if (!externalWindow && window is not null) { ArrayPool<byte>.Shared.Return(window); window = null; } ``` <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *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> > </details> - Fixes adamhathcock/sharpcompress#1020 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 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](https://gh.io/copilot-coding-agent-tips) in the docs. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 22:20:37 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1448