Skip to content

Commit f8af407

Browse files
committed
Fix index-out-of-range crash in romanNumeralNotation for non-diatonic scales
1 parent 601eeb2 commit f8af407

2 files changed

Lines changed: 5 additions & 368 deletions

File tree

Sources/Tonic/Chord.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ public struct Chord: Sendable, Equatable, Hashable, Codable {
9494
/// - Returns: Roman Numeral notation
9595
public func romanNumeralNotation(in key: Key) -> String? {
9696
let capitalRomanNumerals = ["I", "II", "III", "IV", "V", "VI", "VII"]
97-
if let index = key.primaryTriads.firstIndex(where: { $0 == self }) {
97+
// Roman numeral analysis is diatonic (7 degrees). Non-diatonic scales
98+
// (e.g. the 8-note whole/half-diminished) can yield more than 7 primary
99+
// triads, so bounds-check before indexing to avoid an out-of-range trap.
100+
if let index = key.primaryTriads.firstIndex(where: { $0 == self }),
101+
index < capitalRomanNumerals.count {
98102
let romanNumeral = capitalRomanNumerals[index]
99103
switch type {
100104
case .major: return romanNumeral

Sources/Tonic/Chord.swift.orig

Lines changed: 0 additions & 367 deletions
This file was deleted.

0 commit comments

Comments
 (0)