Skip to content

Commit

Permalink
remove panic in ScanStruct
Browse files Browse the repository at this point in the history
  • Loading branch information
garyburd committed Sep 16, 2013
1 parent 5ad4a14 commit fe36254
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion redis/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ func structSpecForType(t reflect.Type) *structSpec {
return ss
}

var scanStructValueError = errors.New("redigo: ScanStruct value must be non-nil pointer to a struct.")

// ScanStruct scans a multi-bulk src containing alternating names and values to
// a struct. The HGETALL and CONFIG GET commands return replies in this format.
//
Expand All @@ -333,9 +335,12 @@ func structSpecForType(t reflect.Type) *structSpec {
func ScanStruct(src []interface{}, dest interface{}) error {
d := reflect.ValueOf(dest)
if d.Kind() != reflect.Ptr || d.IsNil() {
return errors.New("redigo: ScanStruct value must be non-nil pointer")
return scanStructValueError
}
d = d.Elem()
if d.Kind() != reflect.Struct {
return scanStructValueError
}
ss := structSpecForType(d.Type())

if len(src)%2 != 0 {
Expand Down
21 changes: 21 additions & 0 deletions redis/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,27 @@ func TestScanStruct(t *testing.T) {
}
}

func TestBadScanStructArgs(t *testing.T) {
x := []interface{}{"A", "b"}
test := func(v interface{}) {
if err := redis.ScanStruct(x, v); err == nil {
t.Errorf("Expect error for ScanStruct(%T, %T)", x, v)
}
}

test(nil)

var v0 *struct{}
test(v0)

var v1 int
test(&v1)

x = x[:1]
v2 := struct{ A string }{}
test(&v2)
}

var argsTests = []struct {
title string
actual redis.Args
Expand Down

0 comments on commit fe36254

Please sign in to comment.