Skip to content

Commit d149d3f

Browse files
kudavidjackc
authored andcommitted
Fix panic in TryFindUnderlyingTypeScanPlan
Check if CanConvert before calling reflect.Value.Convert
1 parent 046f497 commit d149d3f

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pgtype/pgtype.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ func TryFindUnderlyingTypeScanPlan(dst any) (plan WrappedScanPlanNextSetter, nex
561561
}
562562
}
563563

564-
if nextDstType != nil && dstValue.Type() != nextDstType {
564+
if nextDstType != nil && dstValue.Type() != nextDstType && dstValue.CanConvert(nextDstType) {
565565
return &underlyingTypeScanPlan{dstType: dstValue.Type(), nextDstType: nextDstType}, dstValue.Convert(nextDstType).Interface(), true
566566
}
567567

pgtype/pgtype_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func init() {
3535
// Test for renamed types
3636
type _string string
3737
type _bool bool
38+
type _uint8 uint8
3839
type _int8 int8
3940
type _int16 int16
4041
type _int16Slice []int16
@@ -453,6 +454,14 @@ func TestMapScanNullToWrongType(t *testing.T) {
453454
assert.False(t, pn.Valid)
454455
}
455456

457+
func TestScanToSliceOfRenamedUint8(t *testing.T) {
458+
m := pgtype.NewMap()
459+
var ruint8 []_uint8
460+
err := m.Scan(pgtype.Int2ArrayOID, pgx.TextFormatCode, []byte("{2,4}"), &ruint8)
461+
assert.NoError(t, err)
462+
assert.Equal(t, []_uint8{2, 4}, ruint8)
463+
}
464+
456465
func TestMapScanTextToBool(t *testing.T) {
457466
tests := []struct {
458467
name string

0 commit comments

Comments
 (0)