From 0306ce3a1944d453fc956a091735d6eddf659cef Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 8 Feb 2022 14:13:06 -0600 Subject: [PATCH] Fix scanning negative ints into Int64Scanner --- pgtype/int.go | 6 +++--- pgtype/int.go.erb | 2 +- pgtype/int_test.go | 3 +++ pgtype/int_test.go.erb | 1 + 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pgtype/int.go b/pgtype/int.go index a5b1c0a5..237fe7e7 100644 --- a/pgtype/int.go +++ b/pgtype/int.go @@ -552,7 +552,7 @@ func (scanPlanBinaryInt2ToInt64Scanner) Scan(src []byte, dst interface{}) error return fmt.Errorf("invalid length for int2: %v", len(src)) } - n := int64(binary.BigEndian.Uint16(src)) + n := int64(int16(binary.BigEndian.Uint16(src))) return s.ScanInt64(Int8{Int: n, Valid: true}) } @@ -1100,7 +1100,7 @@ func (scanPlanBinaryInt4ToInt64Scanner) Scan(src []byte, dst interface{}) error return fmt.Errorf("invalid length for int4: %v", len(src)) } - n := int64(binary.BigEndian.Uint32(src)) + n := int64(int32(binary.BigEndian.Uint32(src))) return s.ScanInt64(Int8{Int: n, Valid: true}) } @@ -1670,7 +1670,7 @@ func (scanPlanBinaryInt8ToInt64Scanner) Scan(src []byte, dst interface{}) error return fmt.Errorf("invalid length for int8: %v", len(src)) } - n := int64(binary.BigEndian.Uint64(src)) + n := int64(int64(binary.BigEndian.Uint64(src))) return s.ScanInt64(Int8{Int: n, Valid: true}) } diff --git a/pgtype/int.go.erb b/pgtype/int.go.erb index 8524136f..18e708fa 100644 --- a/pgtype/int.go.erb +++ b/pgtype/int.go.erb @@ -439,7 +439,7 @@ func (scanPlanBinaryInt<%= pg_byte_size %>ToInt64Scanner) Scan(src []byte, dst i } - n := int64(binary.BigEndian.Uint<%= pg_bit_size %>(src)) + n := int64(int<%= pg_bit_size %>(binary.BigEndian.Uint<%= pg_bit_size %>(src))) return s.ScanInt64(Int8{Int: n, Valid: true}) } diff --git a/pgtype/int_test.go b/pgtype/int_test.go index 1dbf32d5..a2e64f4e 100644 --- a/pgtype/int_test.go +++ b/pgtype/int_test.go @@ -22,6 +22,7 @@ func TestInt2Codec(t *testing.T) { {int(1), new(int16), isExpectedEq(int16(1))}, {uint(1), new(int16), isExpectedEq(int16(1))}, {pgtype.Int2{Int: 1, Valid: true}, new(int16), isExpectedEq(int16(1))}, + {int32(-1), new(pgtype.Int2), isExpectedEq(pgtype.Int2{Int: -1, Valid: true})}, {1, new(int8), isExpectedEq(int8(1))}, {1, new(int16), isExpectedEq(int16(1))}, {1, new(int32), isExpectedEq(int32(1))}, @@ -102,6 +103,7 @@ func TestInt4Codec(t *testing.T) { {int(1), new(int32), isExpectedEq(int32(1))}, {uint(1), new(int32), isExpectedEq(int32(1))}, {pgtype.Int4{Int: 1, Valid: true}, new(int32), isExpectedEq(int32(1))}, + {int32(-1), new(pgtype.Int4), isExpectedEq(pgtype.Int4{Int: -1, Valid: true})}, {1, new(int8), isExpectedEq(int8(1))}, {1, new(int16), isExpectedEq(int16(1))}, {1, new(int32), isExpectedEq(int32(1))}, @@ -182,6 +184,7 @@ func TestInt8Codec(t *testing.T) { {int(1), new(int64), isExpectedEq(int64(1))}, {uint(1), new(int64), isExpectedEq(int64(1))}, {pgtype.Int8{Int: 1, Valid: true}, new(int64), isExpectedEq(int64(1))}, + {int32(-1), new(pgtype.Int8), isExpectedEq(pgtype.Int8{Int: -1, Valid: true})}, {1, new(int8), isExpectedEq(int8(1))}, {1, new(int16), isExpectedEq(int16(1))}, {1, new(int32), isExpectedEq(int32(1))}, diff --git a/pgtype/int_test.go.erb b/pgtype/int_test.go.erb index 8858ce90..d55851c2 100644 --- a/pgtype/int_test.go.erb +++ b/pgtype/int_test.go.erb @@ -22,6 +22,7 @@ func TestInt<%= pg_byte_size %>Codec(t *testing.T) { {int(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))}, {uint(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))}, {pgtype.Int<%= pg_byte_size %>{Int: 1, Valid: true}, new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))}, + {int32(-1), new(pgtype.Int<%= pg_byte_size %>), isExpectedEq(pgtype.Int<%= pg_byte_size %>{Int: -1, Valid: true})}, {1, new(int8), isExpectedEq(int8(1))}, {1, new(int16), isExpectedEq(int16(1))}, {1, new(int32), isExpectedEq(int32(1))},