Skip to content

Fix integer overflow in ParallelHelper.For range computation#1202

Open
sayantanmandal1 wants to merge 1 commit into
CommunityToolkit:mainfrom
sayantanmandal1:fix/parallelhelper-for-overflow
Open

Fix integer overflow in ParallelHelper.For range computation#1202
sayantanmandal1 wants to merge 1 commit into
CommunityToolkit:mainfrom
sayantanmandal1:fix/parallelhelper-for-overflow

Conversation

@sayantanmandal1

Copy link
Copy Markdown

Fixes #1188. ParallelHelper.For computed the range size with Math.Abs(start - end) in 32-bit arithmetic, which overflows for ranges larger than int.MaxValue: the subtraction wraps around (silently running single-threaded) and Math.Abs(int.MinValue) throws OverflowException. The range and batch sizes are now computed with 64-bit arithmetic; the per-iteration indices stay within [start, end] so the narrowing casts are safe. Added deterministic regression tests.

Closes #1188

The range size was computed with 'Math.Abs(start - end)' using 32-bit
arithmetic. For ranges larger than int.MaxValue this wraps around (silently
running single-threaded) or throws OverflowException (Math.Abs(int.MinValue)).
Compute the range and batch sizes with 64-bit arithmetic instead.

Closes CommunityToolkit#1188
@sayantanmandal1

Copy link
Copy Markdown
Author

@sayantanmandal1 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree

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.

ParallelHelper.For: integer overflow in range length causes single-threaded fallback and OverflowException on valid inputs

1 participant