Skip to content

Commit

Permalink
Fix a potential segfault in podman search
Browse files Browse the repository at this point in the history
When generating headers for search, we unconditionally
access element 0 of an array, and I saw this segfault in our CI.
There's no reason we have to do this, we're just going through it
to get field names with reflect, so just make a new copy of the
struct in question.

Also, move this code, which is only for CLI display, into
cmd/podman from libpod/image.

Signed-off-by: Matthew Heon <mheon@redhat.com>
  • Loading branch information
mheon committed Apr 3, 2019
1 parent 662ae6c commit c625553
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
16 changes: 15 additions & 1 deletion cmd/podman/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,25 @@ func searchCmd(c *cliconfig.SearchValues) error {
if len(results) == 0 {
return nil
}
out := formats.StdoutTemplateArray{Output: searchToGeneric(results), Template: format, Fields: genSearchOutputMap()}
out := formats.StdoutTemplateArray{Output: searchToGeneric(results), Template: format, Fields: searchHeaderMap()}
formats.Writer(out).Out()
return nil
}

// searchHeaderMap returns the headers of a SearchResult.
func searchHeaderMap() map[string]string {
s := new(image.SearchResult)
v := reflect.Indirect(reflect.ValueOf(s))
values := make(map[string]string, v.NumField())

for i := 0; i < v.NumField(); i++ {
key := v.Type().Field(i).Name
value := key
values[key] = strings.ToUpper(splitCamelCase(value))
}
return values
}

func genSearchFormat(format string) string {
if format != "" {
// "\t" from the command line is not being recognized as a tab
Expand Down
20 changes: 0 additions & 20 deletions libpod/image/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package image

import (
"context"
"reflect"
"strconv"
"strings"
"sync"

"github.com/containers/image/docker"
"github.com/containers/image/types"
sysreg "github.com/containers/libpod/pkg/registries"
"github.com/fatih/camelcase"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
Expand Down Expand Up @@ -63,24 +61,6 @@ type SearchFilter struct {
IsOfficial types.OptionalBool
}

func splitCamelCase(src string) string {
entries := camelcase.Split(src)
return strings.Join(entries, " ")
}

// HeaderMap returns the headers of a SearchResult.
func (s *SearchResult) HeaderMap() map[string]string {
v := reflect.Indirect(reflect.ValueOf(s))
values := make(map[string]string, v.NumField())

for i := 0; i < v.NumField(); i++ {
key := v.Type().Field(i).Name
value := key
values[key] = strings.ToUpper(splitCamelCase(value))
}
return values
}

// SearchImages searches images based on term and the specified SearchOptions
// in all registries.
func SearchImages(term string, options SearchOptions) ([]SearchResult, error) {
Expand Down

0 comments on commit c625553

Please sign in to comment.