fixed issues found by go-fuzz
This commit is contained in:
parent
1674abae73
commit
15d372ac55
3 changed files with 37 additions and 9 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fuzz
|
||||
fuzz.go
|
||||
filter-fuzz.zip
|
||||
18
filter.go
18
filter.go
|
|
@ -102,6 +102,9 @@ func NewQuery(s string, l Logic) (q *Query, err error) {
|
|||
switch s[2] {
|
||||
case asc, desc:
|
||||
q.s.fields = strings.Split(s[1], ".")
|
||||
if err = checkFields(q.s.fields, v); err != nil {
|
||||
return
|
||||
}
|
||||
if len(q.s.fields) > 0 {
|
||||
if s[2] == desc {
|
||||
q.s.desc = true
|
||||
|
|
@ -129,8 +132,12 @@ func NewQuery(s string, l Logic) (q *Query, err error) {
|
|||
}
|
||||
qm[m[0]] = struct{}{}
|
||||
}
|
||||
mf := strings.Split(m[0], ".")
|
||||
if err = checkFields(mf, m[0]); err != nil {
|
||||
return
|
||||
}
|
||||
q.m = append(q.m, match{
|
||||
strings.Split(m[0], "."),
|
||||
mf,
|
||||
matchFunc(m[1]),
|
||||
boolFunc(m[1]),
|
||||
})
|
||||
|
|
@ -191,9 +198,9 @@ func (q *Query) match(v reflect.Value) (ok bool) {
|
|||
if fl == 0 {
|
||||
continue
|
||||
}
|
||||
val := v.FieldByName(qv.fields[0])
|
||||
for j := 1; j < fl; j++ {
|
||||
val = val.FieldByName(qv.fields[j])
|
||||
val := walkFields(v.FieldByName(qv.fields[0]), qv.fields[1:])
|
||||
if val.Kind() == reflect.Invalid {
|
||||
continue
|
||||
}
|
||||
s, b, err := getValue(val)
|
||||
if err != nil {
|
||||
|
|
@ -239,6 +246,9 @@ func (q Query) sort(f Filter) {
|
|||
return
|
||||
}
|
||||
l := makeLess(f, q.s.fields)
|
||||
if l == nil {
|
||||
return
|
||||
}
|
||||
if q.s.desc {
|
||||
l = reverse(l)
|
||||
}
|
||||
|
|
|
|||
25
helper.go
25
helper.go
|
|
@ -12,6 +12,15 @@ const (
|
|||
anys = string(any)
|
||||
)
|
||||
|
||||
func checkFields(s []string, val string) error {
|
||||
for _, v := range s {
|
||||
if v == "" {
|
||||
return errors.New("invalid subfield: " + val)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type less func(i, j int) bool
|
||||
|
||||
func reverse(f less) less {
|
||||
|
|
@ -20,12 +29,18 @@ func reverse(f less) less {
|
|||
}
|
||||
}
|
||||
|
||||
func index(f Filter, i int, fields []string) (val reflect.Value) {
|
||||
val = reflect.ValueOf(f.Index(i)).FieldByName(fields[0])
|
||||
for j := 1; j < len(fields); j++ {
|
||||
val = val.FieldByName(fields[j])
|
||||
func walkFields(v reflect.Value, fields []string) reflect.Value {
|
||||
for i := 0; i < len(fields); i++ {
|
||||
if v.Kind() != reflect.Struct {
|
||||
return reflect.ValueOf(nil)
|
||||
}
|
||||
v = v.FieldByName(fields[i])
|
||||
}
|
||||
return
|
||||
return v
|
||||
}
|
||||
|
||||
func index(f Filter, i int, fields []string) (val reflect.Value) {
|
||||
return walkFields(reflect.ValueOf(f.Index(i)).FieldByName(fields[0]), fields[1:])
|
||||
}
|
||||
|
||||
func makeLess(f Filter, fields []string) less {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue