From d149d3fe5c50d1d98bd6265d3c928519ba4b3f4b Mon Sep 17 00:00:00 2001 From: David Kurman Date: Sun, 25 Feb 2024 17:56:47 +0200 Subject: [PATCH] Fix panic in TryFindUnderlyingTypeScanPlan Check if CanConvert before calling reflect.Value.Convert --- pgtype/pgtype.go | 2 +- pgtype/pgtype_test.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pgtype/pgtype.go b/pgtype/pgtype.go index 08833f876..534ef6d16 100644 --- a/pgtype/pgtype.go +++ b/pgtype/pgtype.go @@ -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 } diff --git a/pgtype/pgtype_test.go b/pgtype/pgtype_test.go index b6e3371ff..c397069b1 100644 --- a/pgtype/pgtype_test.go +++ b/pgtype/pgtype_test.go @@ -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 @@ -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