Skip to content

Fix RAR5 async decompression corruption when executable filters span the 4MB write boundary - #1395

Closed
adamhathcock with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-rar5-decompression-error
Closed

adamhathcock with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-rar5-decompression-error

Conversation

Copilot AI commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

RAR5 decompression via the async reader path (ReaderFactory.OpenAsyncReader) produced corrupted output starting at ~4 MB into certain entries. The sync path was unaffected. Root cause: three divergences between UnpWriteBufAsync and the reference sync UnpWriteBuf that only manifest when an executable filter block (E8/E8E9/ARM) straddles the UNPACK_MAX_WRITE = 4 MB flush boundary.

Bugs fixed in UnpWriteBufAsync

  • WrittenFileSize tracked incorrectly — increment was inside if (OutMem != null), so it was skipped when ApplyFilter returned null (unknown filter type). The sync version always increments it. This corrupts the file-offset argument passed to subsequent E8/E8E9 filter invocations.

  • WrPtr = WrittenBorder missing (most critical) — when a filter block can't be fully written in the current pass (BlockLength > WriteSizeLeft), the sync version saves WrPtr = WrittenBorder to checkpoint the safe-to-output position. The async version omitted this entirely. Since UnpWriteBuf* re-initialises its local WrittenBorder from WrPtr, the next flush cycle started writing from the wrong window offset, producing the corrupted bytes visible in the hex dump.

  • NextWindow set to inverted value — in the same "filter intersects boundary" branch, the sync version resets all remaining filters to NextWindow = false (process next time). The async version set NextWindow = true under different conditions, causing those filters to be incorrectly skipped on the next pass.

// Before (async only — all three bugs):
if (OutMem != null)
{
    await UnpIO_UnpWriteAsync(...);
    WrittenFileSize += BlockLength;   // wrong: skipped when OutMem==null
}
// ...
else  // filter intersects write border
{
    NotAllFiltersProcessed = true;    // WrPtr not saved — critical omission
    if (fltj.NextWindow == false && dist < FullWriteSize)
        fltj.NextWindow = true;       // wrong direction
}

// After (matches sync):
if (OutMem != null)
    await UnpIO_UnpWriteAsync(...);
UnpSomeRead = true;
WrittenFileSize += BlockLength;
// ...
else
{
    WrPtr = WrittenBorder;            // checkpoint written position
    if (fltj.Type != FILTER_NONE)
        fltj.NextWindow = false;      // reset for next pass
    NotAllFiltersProcessed = true;
}

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 changed the title [WIP] Fix RAR5 decompression corruption during entry extraction Fix RAR5 async decompression corruption when executable filters span the 4MB write boundary Aug 1, 2026
Copilot AI requested a review from adamhathcock August 1, 2026 03:40
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.

RAR5 decompression corrupts data partway through an entry

2 participants