Repository navigation
Return false from Duration.TryParse when the reduced fraction overflows int [patch] - #435
Merged
Merged
Conversation
…ws int [patch] "-2147483648/-1" (or any odd negative denominator under int.MinValue) made Create throw OverflowException, which escaped TryParse and every container parser that reads durations through it: Note, Rest, ChordEvent, Section, Progression and Arrangement. Catch it and fail the parse instead, so Parse reports FormatException as for other bad text. Fixes #434 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014uT14TvqqVJCgHTyBKHpKx
|
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 #434
Before:
Duration.TryParse("-2147483648/-1", out _)threwOverflowException. Every parser that reads durations through it threw too:Note,Rest,ChordEvent,Section,ProgressionandArrangement.After:
TryParsereturnsfalse, andParsethrows the sameFormatExceptionit throws for any other bad text.Duration.Create(int.MinValue, -1)still throwsOverflowExceptionas #306 intended.How:
TryParsenow catches theOverflowExceptionfromCreateand returnsfalse. I didn't use the guard the issue suggests (numerator == int.MinValue && denominator < 0), because it is wrong in both directions:-2147483648/-3. Any odd negative denominator leaves 2^31 after reduction, which overflows too.-2147483648/-2, which reduces to 2^30 and fits.Catching the exception lets
Reduceremain the one place that decides what fits.Tests (in
DurationTests)TryParseFailsWhenTheReducedFractionDoesNotFitInIntcovers-2147483648/-1and/-3, for bothTryParseandParse.TryParseAcceptsIntMinValueOverANegativeDenominatorWhenTheReducedFractionFitscovers-2147483648/-2.ContainerTryParseFailsRatherThanThrowingOnAnOverflowingDurationcoversChordEventandProgression.Semantics.Testsuite passes on net10.0 (1703 tests).Semantics.Musicbuilds every target framework with no warnings.Duration.cs. It does report S1192 inMode.csand S3267 inSection.cs, both in files this PR doesn't touch.Generated by Claude Code