Skip to content

Fix RAR5 async decompression corruption in UnpWriteBufAsync - #1396

Merged
adamhathcock merged 3 commits into
releasefrom
adam/fix-rar5-decompression-error
Aug 1, 2026
Merged

adamhathcock merged 3 commits into
releasefrom
adam/fix-rar5-decompression-error

Conversation

@adamhathcock

Copy link
Copy Markdown
Owner

Three bugs in the async version of UnpWriteBuf (used only in the async decompression path) were causing data corruption when decompressing RAR5 archives containing executable filters (E8/E8E9/ARM) whose blocks spanned the 4MB write boundary (UNPACK_MAX_WRITE):

  1. WrittenFileSize was only incremented inside if (OutMem != null), but should always be incremented after ApplyFilter (matching sync behavior).

  2. UnpSomeRead = true was missing from the filter processing path.

  3. In the NotAllFiltersProcessed else branch (filter intersects write border):

    • WrPtr was not updated to WrittenBorder, corrupting the window pointer used to compute WriteBorder on the next flush cycle
    • Remaining filters had NextWindow set to true (inverted logic), when sync correctly sets them to false so they are processed next time

All three changes align the async path with the sync UnpWriteBuf logic.

Fixes: #1394

Supersedes: #1395

Copilot AI and others added 2 commits August 1, 2026 12:19
Three bugs in the async version of UnpWriteBuf (used only in the async
decompression path) were causing data corruption when decompressing RAR5
archives containing executable filters (E8/E8E9/ARM) whose blocks spanned
the 4MB write boundary (UNPACK_MAX_WRITE):

1. WrittenFileSize was only incremented inside `if (OutMem != null)`, but
   should always be incremented after ApplyFilter (matching sync behavior).

2. UnpSomeRead = true was missing from the filter processing path.

3. In the NotAllFiltersProcessed else branch (filter intersects write border):
   - WrPtr was not updated to WrittenBorder, corrupting the window pointer
     used to compute WriteBorder on the next flush cycle
   - Remaining filters had NextWindow set to true (inverted logic), when
     sync correctly sets them to false so they are processed next time

All three changes align the async path with the sync UnpWriteBuf logic.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a RAR5 async decompression corruption bug by bringing UnpWriteBufAsync’s filter/write-boundary logic back in sync with the reference synchronous implementation, addressing the ~4MB boundary corruption described in #1394. Also includes small diagnostics and tooling updates.

Changes:

  • Align UnpWriteBufAsync filter handling with sync behavior (write pointer checkpointing, NextWindow reset, UnpSomeRead, and WrittenFileSize accounting) to prevent corruption when executable filters span the 4MB write boundary.
  • Improve CRC mismatch diagnostics by including the entry key (file name) in the CRC mismatch exception message (with one async overload still needing alignment—see review comment).
  • Bump .NET SDK patch and related lockfile dependencies for ILLink/ILCompiler.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/SharpCompress.AotSmoke/packages.lock.json Updates ILCompiler/ILLink task versions for AOT smoke test lockfile.
src/SharpCompress/packages.lock.json Updates ILLink task versions in the main library lockfile.
src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_async.cs Fixes async unpack write/filter boundary logic to match sync UnpWriteBuf and prevent RAR5 output corruption.
src/SharpCompress/Compressors/Rar/RarCrcStream.cs Refactors fields and improves sync CRC mismatch exception message with entry key.
src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs Refactors async CRC tracking; partially updates CRC mismatch exception message (one overload remains inconsistent).
global.json Bumps pinned .NET SDK patch version (rollForward remains disabled).
Suppressed comments (1)

src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs:43

  • In the byte[] ReadAsync overload, the CRC mismatch exception message is still "file crc mismatch" while the sync path and the Memory async overload now include the entry key (file name). This makes diagnostics inconsistent depending on which overload is used (and which target framework picks which overload).
        else if (
            !_disableCrc
            && GetCrc() != BitConverter.ToUInt32(_readStream.NotNull().CurrentCrc.NotNull(), 0)
            && count != 0
        )
        {
            // NOTE: we use the last FileHeader in a multipart volume to check CRC
            throw new InvalidFormatException("file crc mismatch");
        }

@adamhathcock
adamhathcock merged commit 67bd928 into release Aug 1, 2026
2 of 3 checks passed
@adamhathcock
adamhathcock deleted the adam/fix-rar5-decompression-error branch August 1, 2026 11:50
This was referenced Aug 1, 2026
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