Skip to content

fix: integer overflow in CombinedStream.Write for files >2GB#9

Merged
neon-nyan merged 2 commits into
mainfrom
fix/combined-stream-int-overflow
Jul 11, 2026
Merged

fix: integer overflow in CombinedStream.Write for files >2GB#9
neon-nyan merged 2 commits into
mainfrom
fix/combined-stream-int-overflow

Conversation

@Cryotechnic

@Cryotechnic Cryotechnic commented Jul 10, 2026

Copy link
Copy Markdown
Member

Problem

CombinedStream.Write crashes with ArgumentOutOfRangeException (parameter 'count') when writing to output files larger than ~2.1GB.

The root cause is an unchecked (int) cast:

int remainedMaxLength = (int)(_underlyingStreams[_index].Length - _underlyingStreams[_index].Position);

When Length - Position exceeds Int32.MaxValue (e.g. a 3GB file at position 0), the cast overflows to a large negative number. This negative value is then passed as count to FileStream.Write, which throws.

Affected Scenario

  • Directory-level HDiff patches (HDIFF19 + HDIFF13) producing output files larger than 2GB. Specifically hit with Wuthering Waves pakchunk files ranging from 3-7GB.

Fix

Cap the remaining length to int.MaxValue before the cast using Math.Min. This is safe because count (the bytes to write) is always <= int.MaxValue, so the comparison remainedMaxLength < count still works correctly.\n\nBoth Write(ReadOnlySpan<byte>) and Write(byte[], int, int) overloads are fixed.

Cryotechnic and others added 2 commits July 10, 2026 00:31
The cast (int)(Length - Position) in both Write overloads overflows to a negative value when the remaining bytes in a sub-stream exceed Int32.MaxValue (~2.1GB). This causes FileStream.Write to throw ArgumentOutOfRangeException for the 'count' parameter.

Affected scenario: directory-level krpdiff patches producing output files larger than 2GB (e.g. Wuthering Waves pakchunk files at ~3-7GB).

Fix: use Math.Min(..., int.MaxValue) to cap the value before the cast.
@neon-nyan neon-nyan merged commit a259d86 into main Jul 11, 2026
5 checks passed
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.

2 participants