From 63f7e3e6cc71ff5ba63f84f265262c133aec8d59 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 09:12:37 +0000 Subject: [PATCH] fix(i18n): guard numeric handler fallthrough Co-Authored-By: Virgil --- handler.go | 10 ++++++++-- handler_test.go | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/handler.go b/handler.go index f209692..9e39f74 100644 --- a/handler.go +++ b/handler.go @@ -132,7 +132,10 @@ func (h NumericHandler) Match(key string) bool { func (h NumericHandler) Handle(key string, args []any, next func() string) string { if len(args) == 0 { - return next() + if next != nil { + return next() + } + return "" } format := core.TrimPrefix(key, "i18n.numeric.") switch format { @@ -153,7 +156,10 @@ func (h NumericHandler) Handle(key string, args []any, next func() string) strin } } } - return next() + if next != nil { + return next() + } + return "" } // DefaultHandlers returns the built-in i18n.* namespace handlers. diff --git a/handler_test.go b/handler_test.go index 286dbb0..1464bb9 100644 --- a/handler_test.go +++ b/handler_test.go @@ -266,6 +266,12 @@ func TestNumericHandler(t *testing.T) { if got != "fallback" { t.Errorf("NumericHandler with no args should fallback, got %q", got) } + + // No args and no fallback should not panic. + got = h.Handle("i18n.numeric.number", nil, nil) + if got != "" { + t.Errorf("NumericHandler with no args and no fallback = %q, want empty string", got) + } } func TestCountHandler_UsesLocaleNumberFormat(t *testing.T) { -- 2.45.3