Skip to content

Music factories accept undefined enum values: Pitch.Create(NoteLetter.C, (Accidental)7, 4) returns G4, and Section.Create((SectionType)42, …).ToString() can't be parsed back #533

Closed

Description

@matt-edmondson

What's wrong

The type-safe factories from the music type-safe-factories plan take enums, but they never check that the value is defined:

  • Semantics.Music/Pitch.cs:~45-56: Create passes (int)letter and (int)accidental straight into MidiOf (:~108). The only check is the 0..127 MIDI range.
  • Semantics.Music/PitchClass.cs:~31: same pattern.
  • Semantics.Music/Section.cs:~36-40: Create stores any SectionType.

ChordSymbolWriter.HasSupportedValues (ChordSymbolWriter.cs:~113-118) already guards chord enums with Enum.IsDefined, so this check is a known pattern in the codebase.

Failure scenarios

  • Pitch.Create((NoteLetter)1, Accidental.Natural, 4) returns C#4, and Pitch.Create(NoteLetter.C, (Accidental)7, 4) returns G4. Neither throws. A value coming from deserialised or user-supplied ints silently becomes a different note.
  • Section.Create((SectionType)42, prog).ToString() writes [42]\n4/4 C G, and Section.TryParse rejects that text because TryParseSectionType refuses digits (Section.cs:~283-299). That breaks the plan's Parse(x.ToString()) == x round-trip guarantee.

Suggested fix / acceptance criteria

  • Pitch.Create, PitchClass creation and Section.Create throw ArgumentOutOfRangeException naming the parameter when NoteLetter, Accidental or SectionType is not a defined value.
  • Tests cover each undefined-value case, plus the round trip for every defined SectionType.

Activity

  1. added
    bug
    on Nov 1, 2026
  2. matt-edmondson commented on Nov 1, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions