Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func NewCmdVersionLs() *cobra.Command {
})
}

if flags.limit > 0 {
if flags.limit > 0 && flags.limit < len(filteredTags) {
filteredTags = filteredTags[0:flags.limit]
}
fmt.Println(strings.Join(filteredTags, "\n"))
Expand Down
39 changes: 39 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright © 2020-2023 The k3d Author(s)
*/
package cmd

import "testing"

// Test_VersionLs_LimitClamp_Regression_1662 verifies that the slice expression
// used to apply the `--limit` flag in `k3d version list` does not panic when
// the limit exceeds the number of filtered tags. See issue #1662 where
// `k3d version list -i '^1\.333' -l 1 -o repo k3s` panicked with
// "slice bounds out of range [:1] with capacity 0".
func Test_VersionLs_LimitClamp_Regression_1662(t *testing.T) {
tests := []struct {
name string
tags []string
limit int
expected int
}{
{name: "limit zero leaves slice untouched", tags: []string{"a", "b"}, limit: 0, expected: 2},
{name: "limit smaller than length clamps", tags: []string{"a", "b", "c"}, limit: 2, expected: 2},
{name: "limit equal to length is a no-op", tags: []string{"a", "b"}, limit: 2, expected: 2},
{name: "limit larger than length must not panic", tags: []string{"a"}, limit: 5, expected: 1},
{name: "limit larger than empty slice must not panic", tags: []string{}, limit: 1, expected: 0},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filteredTags := tt.tags
// Mirror the production expression from NewCmdVersionLs.
if tt.limit > 0 && tt.limit < len(filteredTags) {
filteredTags = filteredTags[0:tt.limit]
}
if got := len(filteredTags); got != tt.expected {
t.Fatalf("len(filteredTags) = %d, want %d", got, tt.expected)
}
})
}
}
Loading