diff --git a/libimage/filters.go b/libimage/filters.go index d84511d43..b5e3d3d0d 100644 --- a/libimage/filters.go +++ b/libimage/filters.go @@ -20,31 +20,29 @@ 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 + // Initialize to true for an "AND" logic across all groups + matches := true + for key := range filters { + // Initialize to false for an "AND" logic within a group + groupMatches := false + for _, filter := range filters[key] { var err error - 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 groupMatches, err = filter(i); err != nil { 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 !groupMatches { + return false, nil } } - if !matches { - return false, nil - } + // Update the matches across groups to ensure "AND" logic across all groups + matches = matches && groupMatches } return matches, nil }