From 680df5f205ff6456e56f27ecb1080198977550d4 Mon Sep 17 00:00:00 2001 From: kevin9327 <5299031+kevin9327@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:50:44 +0900 Subject: [PATCH] fix: reject Roman numerals above 3999 in RomanToInt The function documents a maximum of 3999, and IntToRoman already rejects that range, but MMMM was accepted as 4000. Return an error instead and cover it with a test. --- conversion/romantoint.go | 4 ++++ conversion/romantoint_test.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/conversion/romantoint.go b/conversion/romantoint.go index 629952320..c6df21a53 100644 --- a/conversion/romantoint.go +++ b/conversion/romantoint.go @@ -55,5 +55,9 @@ func RomanToInt(input string) (int, error) { if len(input) > 0 { return 0, errors.New("invalid roman numeral") } + // Only standard form in the range 1 to 3999 is supported. + if output > 3999 { + return 0, errors.New("roman numeral must be in range 1 to 3999") + } return output, nil } diff --git a/conversion/romantoint_test.go b/conversion/romantoint_test.go index 0fd34b2c3..41a1b6a39 100644 --- a/conversion/romantoint_test.go +++ b/conversion/romantoint_test.go @@ -43,6 +43,11 @@ func TestRomanToInt(t *testing.T) { if err != nil { t.Errorf("RomanToInt(\"\") returned an error %s", err.Error()) } + + // MMMM is 4000, which is outside the supported 1-3999 range. + if _, err := RomanToInt("MMMM"); err == nil { + t.Error("RomanToInt(MMMM) expected an error") + } } func BenchmarkRomanToInt(b *testing.B) {