fix: integer overflow in CombinedStream.Write for files >2GB#9
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CombinedStream.Writecrashes withArgumentOutOfRangeException(parameter 'count') when writing to output files larger than ~2.1GB.The root cause is an unchecked
(int)cast:When
Length - PositionexceedsInt32.MaxValue(e.g. a 3GB file at position 0), the cast overflows to a large negative number. This negative value is then passed ascounttoFileStream.Write, which throws.Affected Scenario
Fix
Cap the remaining length to
int.MaxValuebefore the cast usingMath.Min. This is safe becausecount(the bytes to write) is always<= int.MaxValue, so the comparisonremainedMaxLength < countstill works correctly.\n\nBothWrite(ReadOnlySpan<byte>)andWrite(byte[], int, int)overloads are fixed.