Repository navigation
Make wrapping arithmetic unchecked - #648
Merged
Merged
Conversation
SimonCropp
force-pushed
the
fix/unchecked-arithmetic
branch
from
October 11, 2026 22:35
14410a6 to
de90bd4
Compare
This was referenced Oct 12, 2026
This was referenced Oct 14, 2026
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.
Bug
Polyfill is compiled into the consuming project, so it inherits the consumer's
CheckForOverflowUnderflowsetting. Several polyfills rely on wrapping arithmetic without saying so, and throwOverflowExceptionin a checked build:BitConverter.UInt32BitsToSingle,UInt64BitsToDouble,SingleToUInt32Bits,DoubleToUInt64Bits: signed/unsigned reinterpreting casts, e.g.SingleToUInt32Bits(-1f)BitOperations.IsPow2(all six overloads, in bothBitOperations.csandBitOperationsIsPow2.cs):value & (value - 1)for0u,int.MinValue,long.MinValue, ...RandomNumberGenerator.GetInt32(fromInclusive, toExclusive):(uint)casts of negative bounds, e.g.GetInt32(-10, -5)Random.NextInt64(minValue, maxValue):maxValue - minValueand the final cast/add for ranges wider thanlong.MaxValue, e.g.NextInt64(long.MinValue, long.MaxValue)Fix
Wrap those expressions in
unchecked(...). No behaviour change in unchecked builds.Test
Added edge-value tests: the four
BitConverter*_NegativeOnetests,BitOperations_IsPow2_NativeMinValue,GetInt32_Range_Negative,GetInt32_Range_Full,RandomNextInt64_NegativeRangeandRandomNextInt64_FullRange(the existingIsPow2tests already cover0uandint.MinValue).The test projects build unchecked, so these only fail before the fix in a checked build. Verified locally with
-p:CheckForOverflowUnderflow=trueon net48: 13 of the 21 selected tests failed withOverflowExceptionbefore the fix and all pass after. In the normal build they pass on net48 and net8.0 (polyfill) and net10.0 (BCL). TheBitOperationsIsPow2.cscopy is only active on netcoreapp3.0 to net6.0, whose runtimes are not installed locally, so that file is covered by CI only.