Skip to content

Commit

Permalink
prevent jmoiron#49 from happening by returning an error when a nil sl…
Browse files Browse the repository at this point in the history
…ice ptr or nil struct ptr has been passed to Select and Get
  • Loading branch information
jmoiron committed Apr 26, 2014
1 parent c6571d8 commit 0c6672b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ func (r *Row) StructScan(dest interface{}) error {
if v.Kind() != reflect.Ptr {
return errors.New("must pass a pointer, not a value, to StructScan destination")
}
if v.IsNil() {
return errors.New("nil pointer passed to StructScan destination")
}

direct := reflect.Indirect(v)
base, err := BaseStructType(direct.Type())
Expand Down Expand Up @@ -938,6 +941,9 @@ func StructScan(rows rowsi, dest interface{}) error {
if value.Kind() != reflect.Ptr {
return errors.New("must pass a pointer, not a value, to StructScan destination")
}
if value.IsNil() {
return errors.New("nil pointer passed to StructScan destination")
}

direct := reflect.Indirect(value)

Expand Down
16 changes: 16 additions & 0 deletions sqlx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,22 @@ func TestEmbeddedStructs(t *testing.T) {
})
}

func TestNilReceiver(t *testing.T) {
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
loadDefaultFixture(db, t)
var p *Person
err := db.Get(p, "SELECT * FROM person LIMIT 1")
if err == nil {
t.Error("Expected error when getting into nil struct ptr.")
}
var pp *[]Person
err = db.Select(pp, "SELECT * FROM person")
if err == nil {
t.Error("Expected an error when selecting into nil slice ptr.")
}
})
}

func TestNamedQuery(t *testing.T) {
var schema = Schema{
create: `
Expand Down

0 comments on commit 0c6672b

Please sign in to comment.