Fix StackOverflowException in Precision.Increment/Decrement for count == int.MinValue#1154
Open
sailro wants to merge 1 commit into
Open
Conversation
… == int.MinValue Increment(value, count) and Decrement(value, count) delegate a negative count to the sibling with the negated count (Decrement(value, -count) / Increment(value, -count)). For count == int.MinValue, -count overflows back to int.MinValue, so the two methods recurse into each other forever and throw an uncatchable StackOverflowException. Move the logic into long-count overloads and have the public int methods delegate, so the negation is done in long: -(long)int.MinValue == 2147483648 is representable, and a count of int.MinValue now steps |int.MinValue| representable values in the opposite direction like any other negative count. Behavior is unchanged for every other count (verified by differential check over a grid of values). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Fixes #1153.
Problem
Precision.Increment(double, int)andPrecision.Decrement(double, int)throw an uncatchableStackOverflowExceptionwhencount == int.MinValue. Each method delegates a negative count to its sibling with the negated count, and-int.MinValueoverflows back toint.MinValue, so the two methods recurse into each other forever:Fix
Move the logic into private
long-count overloads; the publicintmethods delegate to them. The negation is then performed inlong, where-(long)int.MinValue == 2147483648is representable, so a count ofint.MinValuesteps|int.MinValue|representable values in the opposite direction like any other negative count.Behavior is unchanged for every other
(value, count)— the public API is identical and the only previously-reachable difference is thatint.MinValuecrashed. I verified this with a differential check comparing the old and new implementations over a grid of boundary values (0,±0.0,±1,±ε,MaxValue,MinValue,NaN,±∞, …) × counts (0, ±1, ±2, ±5, ±1e6, ±int.MaxValue): bit-identical for every pair.Test
Added
PrecisionTest.IncrementDecrementWithIntMinValueCount, which asserts theint.MinValuecase steps the bit pattern by2147483648in the correct direction (and, by completing at all, that it no longer overflows the stack). The fullPrecisionTestclass passes (162 tests).