Skip to content

Commit cd4a62a

Browse files
authored
Merge pull request containerd#3469 from xyz-li/main
[fix] filter images panic
2 parents b94c658 + 12e63b5 commit cd4a62a

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

cmd/nerdctl/image/image_list_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ LABEL version=0.1`, testutil.CommonImage)
110110
base.Cmd("images", "--filter", fmt.Sprintf("since=%s", testutil.CommonImage)).AssertOutNotContains(testutil.ImageRepo(testutil.CommonImage))
111111
base.Cmd("images", "--filter", fmt.Sprintf("since=%s", testutil.CommonImage), testutil.CommonImage).AssertOutNotContains(testutil.ImageRepo(testutil.CommonImage))
112112
base.Cmd("images", "--filter", fmt.Sprintf("since=%s", testutil.CommonImage), testutil.CommonImage).AssertOutNotContains(tempName)
113+
base.Cmd("images", "--filter", fmt.Sprintf("since=%s:%s", "non-exists-image", "non-exists-image")).AssertOutContains(tempName)
114+
base.Cmd("images", "--filter", fmt.Sprintf("before=%s:%s", "non-exists-image", "non-exists-image")).AssertOutContains(tempName)
113115
}
114116
base.Cmd("images", "--filter", "label=foo=bar").AssertOutContains(tempName)
115117
base.Cmd("images", "--filter", "label=foo=bar1").AssertOutNotContains(tempName)

pkg/imgutil/filtering.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,12 @@ func FilterByCreatedAt(ctx context.Context, client *containerd.Client, before []
150150
if err != nil {
151151
return []images.Image{}, err
152152
}
153-
maxTime = beforeImages[0].CreatedAt
154-
for _, image := range beforeImages {
155-
if image.CreatedAt.After(maxTime) {
156-
maxTime = image.CreatedAt
153+
if len(beforeImages) > 0 {
154+
maxTime = beforeImages[0].CreatedAt
155+
for _, image := range beforeImages {
156+
if image.CreatedAt.After(maxTime) {
157+
maxTime = image.CreatedAt
158+
}
157159
}
158160
}
159161
}
@@ -163,10 +165,12 @@ func FilterByCreatedAt(ctx context.Context, client *containerd.Client, before []
163165
if err != nil {
164166
return []images.Image{}, err
165167
}
166-
minTime = sinceImages[0].CreatedAt
167-
for _, image := range sinceImages {
168-
if image.CreatedAt.Before(minTime) {
169-
minTime = image.CreatedAt
168+
if len(sinceImages) > 0 {
169+
minTime = sinceImages[0].CreatedAt
170+
for _, image := range sinceImages {
171+
if image.CreatedAt.Before(minTime) {
172+
minTime = image.CreatedAt
173+
}
170174
}
171175
}
172176
}

0 commit comments

Comments
 (0)