Skip to content

Commit

Permalink
Fix panic in TryFindUnderlyingTypeScanPlan
Browse files Browse the repository at this point in the history
Check if CanConvert before calling reflect.Value.Convert
  • Loading branch information
kudavid authored and jackc committed Feb 26, 2024
1 parent 046f497 commit d149d3f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pgtype/pgtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 9 additions & 0 deletions pgtype/pgtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
// Test for renamed types
type _string string
type _bool bool
type _uint8 uint8
type _int8 int8
type _int16 int16
type _int16Slice []int16
Expand Down Expand Up @@ -453,6 +454,14 @@ func TestMapScanNullToWrongType(t *testing.T) {
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) {
tests := []struct {
name string
Expand Down

0 comments on commit d149d3f

Please sign in to comment.