From af716aa9fb8a44417c8b893b844c3d8f1908c8c2 Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Sun, 30 Aug 2026 18:21:59 +0300 Subject: [PATCH] internal: fix MaxSizeSubjectsListForLog off-by-one when maxToDisplay is 0 The loop appended before checking the length, so with maxToDisplay=0 one domain leaked into the output before the break check. Reorder so the length check comes first. Signed-off-by: Mohammed Al Sahaf --- internal/logs.go | 2 +- internal/ranges_test.go | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/internal/logs.go b/internal/logs.go index 4ed4a572e..9f8af0553 100644 --- a/internal/logs.go +++ b/internal/logs.go @@ -10,10 +10,10 @@ func MaxSizeSubjectsListForLog(subjects map[string]struct{}, maxToDisplay int) [ numberOfNamesToDisplay := min(len(subjects), maxToDisplay) domainsToDisplay := make([]string, 0, numberOfNamesToDisplay) for domain := range subjects { - domainsToDisplay = append(domainsToDisplay, domain) if len(domainsToDisplay) >= numberOfNamesToDisplay { break } + domainsToDisplay = append(domainsToDisplay, domain) } if len(subjects) > maxToDisplay { domainsToDisplay = append(domainsToDisplay, fmt.Sprintf("(and %d more...)", len(subjects)-maxToDisplay)) diff --git a/internal/ranges_test.go b/internal/ranges_test.go index fff952283..d9ebd3649 100644 --- a/internal/ranges_test.go +++ b/internal/ranges_test.go @@ -90,12 +90,8 @@ func TestMaxSizeSubjectsListForLog(t *testing.T) { "b.com": {}, }, maxToDisplay: 0, - // BUG: When maxToDisplay is 0, code still appends one domain - // because append happens before the break check in the loop. - // Expected behavior: 1 item (just suffix). Actual: 2 items - // (1 leaked domain + suffix). - wantLen: 2, - wantSuffix: true, + wantLen: 1, + wantSuffix: true, }, { name: "single subject with max 1",