Skip to content

Fix possible overflow in WriteToStream - #137242

Merged
elinor-fung merged 1 commit into
dotnet:mainfrom
HEllRZA:bugfix/137187-fix-overflow-in-WriteToStream
Nov 11, 2026
Merged

elinor-fung merged 1 commit into
dotnet:mainfrom
HEllRZA:bugfix/137187-fix-overflow-in-WriteToStream

Conversation

@HEllRZA

@HEllRZA HEllRZA commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

WriteToStream casts the file length from long to int before
calculating the remaining bytes. For files larger than int.MaxValue
(2 GiB), this can produce a negative bytesRequested,
causing the method to exit without reporting an error or copying the
complete file. The int position counter also cannot represent offsets
beyond this limit.

This fix uses a long position counter and calculates the remaining
length using 64-bit arithmetic. It casts the result to int only after
Math.Min caps the requested size to the 16 KiB buffer.

fixes #137187

WriteToStream contains a cast of `long length` to `int`.
This leads to an overflow when the incoming file length is > 2 GB.
`(int)length - pos` becomes negative and `bytesRequested` gets negative.
The code earlies out in the condition `if (bytesRequested <= 0)` without
an error.

The fix correctly calculates `length - pos` as long and only
casts the result of the Math.Min operation to int. As the 2nd
parameter is small (bufSize = 16k); there should not be an overflow
of `bytesRequested` anymore until we reach a file length of 8192 PB.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 10, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@elinor-fung elinor-fung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@elinor-fung
elinor-fung merged commit 43cf30e into dotnet:main Nov 11, 2026
82 checks passed
@elinor-fung

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@elinor-fung

Copy link
Copy Markdown
Member

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@elinor-fung

Copy link
Copy Markdown
Member

@HEllRZA Were you able to confirm that nothing else in your E2E scenario fails after this fix? That is, once we correctly produce a 2GB+ binary, does it run properly?

We're looking at backporting and would like to check that it fixes your full scenario (beyond the reflection-based minimal repro you provided in the issue).

@HEllRZA

HEllRZA commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@elinor-fung
Yes that fix solves it and no other issue appeared.. The creation of our 2.9 GB installer was successful. It has the correct final size and can be executed.

JulieLeeMSFT pushed a commit that referenced this pull request Nov 21, 2026
…137383)

Backport of #137242 to release/11.0

/cc @elinor-fung @HEllRZA

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Issue: #137187

Publishing as single-file produces an empty executable above 2GB. There
is no warning/error about the incorrect output.

## Regression

- [x] Yes
- [ ] No

Pre-existing bug, but exposed via changes in how the bundler writes out
the single-file host in .NET 10.

## Testing

Manual

## Risk

Low

Co-authored-by: Bernd Eckstein <[email protected]>
JulieLeeMSFT pushed a commit that referenced this pull request Nov 21, 2026
…137384)

Backport of #137242 to release/10.0

/cc @elinor-fung @HEllRZA

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Issue: #137187

Publishing as single-file produces an empty executable above 2GB. There
is no warning/error about the incorrect output.

## Regression

- [x] Yes
- [ ] No

Pre-existing bug, but exposed via changes in how the bundler writes out
the single-file host in .NET 10.

## Testing

Manual

## Risk

Low

Co-authored-by: Bernd Eckstein <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Host community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET 10 single-file publishing silently produces empty executables above 2 GiB due to integer overflow in BinaryUtils.WriteToStream

2 participants