From a7f368f61daea081bba8d3870d8ce8dc522335f6 Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Mon, 25 May 2026 17:52:35 +0200 Subject: [PATCH] cli-plugins/hooks: fix max-message off-by-one --- cli-plugins/hooks/template.go | 2 +- cli-plugins/hooks/template_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cli-plugins/hooks/template.go b/cli-plugins/hooks/template.go index eca5ce4f7227..d41c1c3f4540 100644 --- a/cli-plugins/hooks/template.go +++ b/cli-plugins/hooks/template.go @@ -40,7 +40,7 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) { } out = b.String() } - if n := strings.Count(out, "\n"); n > maxMessages { + if n := strings.Count(out, "\n") + 1; n > maxMessages { return nil, fmt.Errorf("hook template contains too many messages (%d): maximum is %d", n, maxMessages) } return strings.SplitN(out, "\n", maxMessages), nil diff --git a/cli-plugins/hooks/template_test.go b/cli-plugins/hooks/template_test.go index 28b290696f1d..982742c7d836 100644 --- a/cli-plugins/hooks/template_test.go +++ b/cli-plugins/hooks/template_test.go @@ -1,6 +1,7 @@ package hooks_test import ( + "strings" "testing" "github.com/docker/cli/cli-plugins/hooks" @@ -123,3 +124,10 @@ func TestParseTemplate(t *testing.T) { }) } } + +func TestParseTemplateTooManyMessages(t *testing.T) { + testCmd := &cobra.Command{Use: "pull"} + + _, err := hooks.ParseTemplate(strings.Repeat("line\n", 10)+"line", testCmd) + assert.Error(t, err, "hook template contains too many messages (11): maximum is 10") +}