Skip to content

Comments

Fix NullReferenceException when extracting 7z empty-stream entries#1219

Merged
adamhathcock merged 5 commits intoreleasefrom
copilot/fix-nullreferenceexception-7z-extraction
Feb 14, 2026
Merged

Fix NullReferenceException when extracting 7z empty-stream entries#1219
adamhathcock merged 5 commits intoreleasefrom
copilot/fix-nullreferenceexception-7z-extraction

Conversation

Copy link
Contributor

Copilot AI commented Feb 14, 2026

Extracting 7z archives containing zero-byte files throws NullReferenceException in SevenZipArchive.GetEntryStream(). Empty-stream entries (files with HasStream == false) have Folder = null, but the code dereferenced folder! without checking.

Changes

  • Core fix: Added null check in SevenZipArchive.GetEntryStream() to return Stream.Null for empty-stream entries, consistent with directory handling
  • Test coverage: Added 7Zip.EmptyStream.7z test archive (4 empty files) and verification test
var folder = entry.FilePart.Folder;

// If folder is null (empty stream entry), return empty stream
if (folder is null)
{
    return CreateEntryStream(Stream.Null);
}

// ... existing folder stream handling

Empty-stream entries are a valid 7z construct where files contain no compressed data. The Folder property is null for such entries by design (see SevenZipFilePart constructor).

Original prompt

This section details on the original issue you should resolve

<issue_title>NullReferenceException when extracting 7z with empty-stream entry via WriteToDirectory</issue_title>
<issue_description>## Summary
Extracting a specific .7z archive throws NullReferenceException in SharpCompress 0.46.0 when calling
WriteToDirectory.

Repro project

https://github.com/YoshihiroIto/SharpCompressRepro_SevenZip_Extraction_Failure

Steps to reproduce

  1. Clone the repro project.
  2. Run:
    dotnet run
    
  3. The app uses archive00.7z and calls archive.WriteToDirectory(...).

Actual behavior

A System.NullReferenceException is thrown from:

  • SharpCompress.Common.SevenZip.ArchiveDatabase.GetFolderStream(...)
  • called via SharpCompress.Archives.SevenZip.SevenZipArchive.SevenZipReader.GetEntryStream()

Expected behavior

Extraction should succeed without exception.
For an empty-stream file entry, SharpCompress should create a zero-byte file (or otherwise handle it safely), not
crash.

Environment

  • SharpCompress: 0.46.0
  • .NET SDK: 10.0.103
  • OS: Windows (repro project environment)

Notes

This appears related to 7z entries where HasStream == false (valid empty file entry).
In that case, folder-based stream handling may dereference a null folder.
</issue_description>

<agent_instructions>Redo #1218 but on release</agent_instructions>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 14, 2026 10:14
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullReferenceException during extraction of 7z archives Fix NullReferenceException when extracting 7z empty-stream entries Feb 14, 2026
Copilot AI requested a review from adamhathcock February 14, 2026 10:21
@adamhathcock adamhathcock marked this pull request as ready for review February 14, 2026 10:22
@kiloconnect
Copy link
Contributor

kiloconnect bot commented Feb 14, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (3 files)
  • src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs
  • src/SharpCompress/IO/SharpCompressStream.cs
  • tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs

@adamhathcock adamhathcock merged commit 1fddd10 into release Feb 14, 2026
3 checks passed
@adamhathcock adamhathcock deleted the copilot/fix-nullreferenceexception-7z-extraction branch February 14, 2026 10:43
@YoshihiroIto
Copy link

Hi @adamhathcock, thanks for getting this fix merged quickly (especially on the release branch).

I noticed PR #1219 is effectively a rework/port of my original PR #1218 (“redo #1218 but on release”), and #1218 ended up closed without attribution carrying over.

Also, as a factual detail, the reproduction test data file 7Zip.EmptyStream.7z used here is the exact file I provided in #1218.

Would you mind adding a short credit/reference for traceability? For example, adding a line to the PR description like:
“Originally contributed by @YoshihiroIto in #1218 (ported to release in #1219).”

Thanks!

@adamhathcock
Copy link
Owner

I wasn't trying to not credit you, I just let the robots do their thing referencing your PR but on the release branch and I manually merged the fix into master

Originally contributed by @YoshihiroIto in #1218 (ported to release in #1219).

@YoshihiroIto
Copy link

Hi @adamhathcock,

Thanks for clarifying — much appreciated!
And thanks for adding the attribution line. 🙏

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.

3 participants