Fix crash with pointer to nil struct

pull/1185/head
Jack Christensen 2022-03-22 19:20:22 -05:00
parent 9f23ed84ba
commit 5ca048ed2d
2 changed files with 15 additions and 1 deletions

View File

@ -952,7 +952,12 @@ func TryWrapStructScanPlan(target interface{}) (plan WrappedScanPlanNextSetter,
return nil, nil, false
}
targetElemValue := targetValue.Elem()
var targetElemValue reflect.Value
if targetValue.IsNil() {
targetElemValue = reflect.New(targetValue.Type().Elem())
} else {
targetElemValue = targetValue.Elem()
}
targetElemType := targetElemValue.Type()
if targetElemType.Kind() == reflect.Struct {

View File

@ -175,6 +175,15 @@ func TestTypeMapScanUnregisteredOIDToCustomType(t *testing.T) {
assert.Nil(t, pCt)
}
func TestTypeMapScanPointerToNilStructDoesNotCrash(t *testing.T) {
m := pgtype.NewMap()
type myStruct struct{}
var p *myStruct
err := m.Scan(0, pgx.TextFormatCode, []byte("(foo,bar)"), &p)
require.NotNil(t, err)
}
func TestTypeMapScanUnknownOIDTextFormat(t *testing.T) {
m := pgtype.NewMap()