From 6ee393b369257a83fa84b96b820f1fb7e8c00ecb Mon Sep 17 00:00:00 2001 From: Urvashi Mohnani Date: Mon, 15 Jan 2024 08:30:40 -0500 Subject: [PATCH] Fix Image.applyFilters to do an AND logic When multiple filters are given, only return objects that match all the filters given by the user. This matches Docker behavior. Signed-off-by: Urvashi Mohnani --- libimage/filters.go | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/libimage/filters.go b/libimage/filters.go index d84511d435..1b29837984 100644 --- a/libimage/filters.go +++ b/libimage/filters.go @@ -20,33 +20,25 @@ import ( // indicates that the image matches the criteria. type filterFunc func(*Image) (bool, error) -// Apply the specified filters. At least one filter of each key must apply. +// Apply the specified filters. All filters of each key must apply. func (i *Image) applyFilters(ctx context.Context, filters map[string][]filterFunc) (bool, error) { - matches := false - for key := range filters { // and - matches = false - for _, filter := range filters[key] { // or - var err error - matches, err = filter(i) + for key := range filters { + for _, filter := range filters[key] { + matches, err := filter(i) if err != nil { - // Some images may have been corrupted in the - // meantime, so do an extra check and make the - // error non-fatal (see containers/podman/issues/12582). if errCorrupted := i.isCorrupted(ctx, ""); errCorrupted != nil { logrus.Errorf(errCorrupted.Error()) return false, nil } return false, err } - if matches { - break + // If any filter within a group doesn't match, return false + if !matches { + return false, nil } } - if !matches { - return false, nil - } } - return matches, nil + return true, nil } // filterImages returns a slice of images which are passing all specified