Fix panic in TryFindUnderlyingTypeScanPlan

Check if CanConvert before calling reflect.Value.Convert
pull/1925/head
David Kurman 2024-02-25 17:56:47 +02:00 committed by Jack Christensen
parent 046f497efb
commit d149d3fe5c
2 changed files with 10 additions and 1 deletions

View File

@ -561,7 +561,7 @@ func TryFindUnderlyingTypeScanPlan(dst any) (plan WrappedScanPlanNextSetter, nex
} }
} }
if nextDstType != nil && dstValue.Type() != nextDstType { if nextDstType != nil && dstValue.Type() != nextDstType && dstValue.CanConvert(nextDstType) {
return &underlyingTypeScanPlan{dstType: dstValue.Type(), nextDstType: nextDstType}, dstValue.Convert(nextDstType).Interface(), true return &underlyingTypeScanPlan{dstType: dstValue.Type(), nextDstType: nextDstType}, dstValue.Convert(nextDstType).Interface(), true
} }

View File

@ -35,6 +35,7 @@ func init() {
// Test for renamed types // Test for renamed types
type _string string type _string string
type _bool bool type _bool bool
type _uint8 uint8
type _int8 int8 type _int8 int8
type _int16 int16 type _int16 int16
type _int16Slice []int16 type _int16Slice []int16
@ -453,6 +454,14 @@ func TestMapScanNullToWrongType(t *testing.T) {
assert.False(t, pn.Valid) assert.False(t, pn.Valid)
} }
func TestScanToSliceOfRenamedUint8(t *testing.T) {
m := pgtype.NewMap()
var ruint8 []_uint8
err := m.Scan(pgtype.Int2ArrayOID, pgx.TextFormatCode, []byte("{2,4}"), &ruint8)
assert.NoError(t, err)
assert.Equal(t, []_uint8{2, 4}, ruint8)
}
func TestMapScanTextToBool(t *testing.T) { func TestMapScanTextToBool(t *testing.T) {
tests := []struct { tests := []struct {
name string name string