mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-03 21:23:38 +00:00
[PR #1104] [MERGED] Fix InvalidOperationException when RAR uncompressed size exceeds header value #1534
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/adamhathcock/sharpcompress/pull/1104
Author: @Copilot
Created: 1/4/2026
Status: ✅ Merged
Merged: 1/4/2026
Merged by: @adamhathcock
Base:
master← Head:copilot/fix-invalidoperationexception-rar📝 Commits (6)
bb66100Initial planb25493fFix InvalidOperationException when RAR unpacks more data than header specifies83b1125Add test for InvalidOperationException fix in RAR extraction1257479Update test to document exception behavior for RAR stream validation90a33ceRemove duplicate test that doesn't verify exception case41ed4c8Add negative test case for premature stream termination📊 Changes
3 files changed (+142 additions, -2 deletions)
View changed files
📝
src/SharpCompress/Compressors/Rar/RarStream.cs(+2 -2)➕
tests/SharpCompress.Test/Mocks/TruncatedStream.cs(+65 -0)📝
tests/SharpCompress.Test/Rar/RarArchiveTests.cs(+75 -0)📄 Description
Changes Made
Core Fix:
RarStream.Read()andRarStream.ReadImplAsync()from_position != Lengthto_position < LengthTest Coverage:
Rar_StreamValidation_OnlyThrowsOnPrematureEnd): Validates normal extraction succeeds across RAR, RAR5, RAR4, and RAR2 formats without throwing when position >= expected lengthRar_StreamValidation_ThrowsOnTruncatedStream): VerifiesInvalidOperationExceptionIS thrown when a RAR stream ends prematurely (position < expected length)TruncatedStream): Created to simulate corrupted/truncated RAR files for negative testingThis allows successful extraction of RAR archives where headers underestimate the uncompressed size, while still catching truncated/corrupted extractions.
<issue_title>InvalidOperationException when extracting RAR - 0.42.0, 0.42.1, 0.43.0</issue_title>
><issue_description>Extracting RAR files now raise InvalidOperationException
>
> Message:
> System.InvalidOperationException : unpacked file size does not match header: expected 22369776 found 31148016
>
> Stack Trace:
> RarStream.ReadImplAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
> RarStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
> Stream.</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
>
><comment_new>@adamhathcock
> I assume this worked with versions earlier than 0.42.0?
>
> What OS and can you give me a sample file?</comment_new>
>
Original prompt
💡 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.