Repository navigation
[patch] Keep Tempo.TryParse from throwing, and reject infinite tempos and non-positive beats - #401
Merged
Merged
Conversation
… and non-positive beats Fixes #305 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019RYN9jT6szUiySkywFKCA1
|
This was referenced Oct 9, 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.



Fixes #305
What was wrong
Tempohad two validation gaps:TryParsecould throw. Its guard wasbpm <= 0.0, which is false for NaN, so"NaNbpm@1/4"reachedCreate, andCreatethrewArgumentOutOfRangeException. That breaks the Try-pattern contract for untrusted text.Secondsmeaningless were accepted. An infinite tempo, and a zero or negative beat (0/4,-1/4), passed bothTryParseandCreate.Secondsdivides by the beat, so these gave Infinity or negative time.Change (
Semantics.Music/Tempo.cs)IsValidBeatsPerMinute(positive and finite) andIsValidBeat(Numerator > 0;Durationnormalises the sign onto the numerator, so1/-4is caught too).CreatethrowsArgumentOutOfRangeExceptionfor a non-finite or non-positive tempo, and for a non-positive beat (after the existing null check).TryParseapplies the same predicates itself, so it returnsfalsefor these inputs and never reaches a throwingCreate.The triage also mentioned
Duration.TryParseaccepting0/4and-1/4. I leftDurationalone because zero and negative durations may be legitimate for other consumers (for example, differences). This PR only stopsTempofrom using one as its beat.Tests (
TempoTests)TryParseReturnsFalseForInvalidTempo:NaN,-NaN,±Infinity,0, and-120bpm, plus0/4,-1/4and1/-4beats. Each returnsfalsewith a null result.TryParseAcceptsValidTempo:"120bpm@1/4"still parses and round-trips.CreateThrowsForInvalidBeatsPerMinute: NaN, ±Infinity, 0 and -1.CreateThrowsForNonPositiveBeat:0/1,0/4,-1/4and1/-4.With
Tempo.csreverted, 11 of these cases fail. With the fix, the fullSemantics.Testsuite passes: 1499 passed, 0 failed, 8 skipped.Semantics.Musicbuilds for every target framework.🤖 Generated with Claude Code
https://claude.ai/code/session_019RYN9jT6szUiySkywFKCA1
Generated by Claude Code