Skip to content

Refuse a section header outside the documented grammar [patch] - #497

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/section-header-strict
Oct 30, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/section-header-strict

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #492

What changed

Section.TryParse used to find a label quote anywhere after ], then look for the key ( only after the label. It never checked the text in between or after. A chart that wrote [Verse] (C major) "Verse 1" therefore lost its local key without an error, and junk anywhere in the header was accepted. Arrangement.Parse reads section headers through the same method, so it had the same gap.

After ] the header must now be exactly one of the following. These are the forms ToString writes:

  • nothing
  • "<label>"
  • (<key>)
  • "<label>" (<key>)

The new TryParseHeaderTail, TrySkipSeparator and TryReadKey helpers do the reading, which keeps TryParse under Sonar's cognitive-complexity limit (S3776).

Tests

StrictParsingTests gains:

  • Section_RejectsAHeaderOutsideTheGrammar covers the four inputs from the issue, plus a stray trailing or doubled space and a missing separator.
  • Section_StillParsesALabelAndKey checks that [Verse] "Verse 1" (C major) still comes back with its type, label and key.

Verification

  • Full Semantics.Test suite: 1954 passed, 14 skipped, 0 failed.
  • With the Section.cs change reverted, all 9 rejection rows fail.
  • I ran the local Sonar analyzers (.sonarlint/sonar-local.props) on Semantics.Music. They report nothing new in Section.cs. The S3267 loop in TryParseSectionType and the S1192 in Mode.cs were already there on main.

Generated by Claude Code

Section.TryParse looked for a label quote anywhere after "]", then for the
key "(" only after the label, and never checked what lay between or after
them. So "[Verse] (C major) \"Verse 1\"" parsed with its key silently
dropped, and junk anywhere in the header was accepted. Arrangement.Parse
reads its section headers through the same method.

After "]" the header now has to be exactly nothing, ` "<label>"`,
` (<key>)` or ` "<label>" (<key>)`, matching what ToString writes.

Fixes #492

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW4o1SWh5jxZ3HQXYvx
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 919af9d into main Oct 30, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/section-header-strict branch October 30, 2026 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Section.Parse silently drops a key written before the label and accepts junk anywhere in the header

2 participants