Skip to content

Guard against overflow in SubReadStream#128074

Open
rzikm wants to merge 1 commit into
dotnet:mainfrom
rzikm:fix/subreadstream-checked-arithmetic
Open

Guard against overflow in SubReadStream#128074
rzikm wants to merge 1 commit into
dotnet:mainfrom
rzikm:fix/subreadstream-checked-arithmetic

Conversation

@rzikm
Copy link
Copy Markdown
Member

@rzikm rzikm commented May 12, 2026

No description provided.

Copilot AI review requested due to automatic review settings May 12, 2026 12:31
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens SubReadStream implementations used by ZIP and TAR readers by making the _endInSuperStream computation overflow-checked, preventing silent wraparound when startPosition + maxLength exceeds long.MaxValue.

Changes:

  • Add checked(startPosition + maxLength) when computing _endInSuperStream in System.IO.Compression’s ZipCustomStreams.SubReadStream.
  • Add the same checked arithmetic in System.Formats.Tar’s SubReadStream.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs Adds overflow-checked end-position calculation for ZIP entry substreams.
src/libraries/System.Formats.Tar/src/System/Formats/Tar/SubReadStream.cs Adds overflow-checked end-position calculation for TAR entry substreams.

Comment on lines 264 to 268
{
_startInSuperStream = startPosition;
_positionInSuperStream = startPosition;
_endInSuperStream = startPosition + maxLength;
_endInSuperStream = checked(startPosition + maxLength);
_superStream = superStream;
_startInSuperStream = startPosition;
_positionInSuperStream = startPosition;
_endInSuperStream = startPosition + maxLength;
_endInSuperStream = checked(startPosition + maxLength);
Comment on lines 264 to 268
{
_startInSuperStream = startPosition;
_positionInSuperStream = startPosition;
_endInSuperStream = startPosition + maxLength;
_endInSuperStream = checked(startPosition + maxLength);
_superStream = superStream;
_startInSuperStream = startPosition;
_positionInSuperStream = startPosition;
_endInSuperStream = startPosition + maxLength;
_endInSuperStream = checked(startPosition + maxLength);
Copy link
Copy Markdown
Contributor

@iremyux iremyux left a comment

Choose a reason for hiding this comment

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

LGTM, would adding some tests be useful in this case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants