From 99bfc154f0ff1da92d35ea4d7a07c45d219e61f3 Mon Sep 17 00:00:00 2001 From: Manni Wood Date: Sat, 3 Sep 2016 18:19:33 -0400 Subject: [PATCH 1/3] Makes Oid casting consistent Also fixes uint32 encoding in a few places. --- messages.go | 6 ++++++ values.go | 10 +++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/messages.go b/messages.go index 7e5c3b54..c158d5d2 100644 --- a/messages.go +++ b/messages.go @@ -138,6 +138,12 @@ func (wb *WriteBuf) WriteInt32(n int32) { wb.buf = append(wb.buf, b...) } +func (wb *WriteBuf) WriteUint32(n uint32) { + b := make([]byte, 4) + binary.BigEndian.PutUint32(b, n) + wb.buf = append(wb.buf, b...) +} + func (wb *WriteBuf) WriteInt64(n int64) { b := make([]byte, 8) binary.BigEndian.PutUint64(b, uint64(n)) diff --git a/values.go b/values.go index ee43813b..a1d2b515 100644 --- a/values.go +++ b/values.go @@ -1299,19 +1299,19 @@ func decodeInt4(vr *ValueReader) int32 { func decodeOid(vr *ValueReader) Oid { if vr.Len() == -1 { vr.Fatal(ProtocolError("Cannot decode null into Oid")) - return 0 + return Oid(0) } if vr.Type().DataType != OidOid { vr.Fatal(ProtocolError(fmt.Sprintf("Cannot decode oid %v into pgx.Oid", vr.Type().DataType))) - return 0 + return Oid(0) } // Oid needs to decode text format because it is used in loadPgTypes switch vr.Type().FormatCode { case TextFormatCode: s := vr.ReadString(vr.Len()) - n, err := strconv.ParseInt(s, 10, 32) + n, err := strconv.ParseUint(s, 10, 32) if err != nil { vr.Fatal(ProtocolError(fmt.Sprintf("Received invalid Oid: %v", s))) } @@ -1319,7 +1319,7 @@ func decodeOid(vr *ValueReader) Oid { case BinaryFormatCode: if vr.Len() != 4 { vr.Fatal(ProtocolError(fmt.Sprintf("Received an invalid size for an Oid: %d", vr.Len()))) - return 0 + return Oid(0) } return Oid(vr.ReadInt32()) default: @@ -1334,7 +1334,7 @@ func encodeOid(w *WriteBuf, oid Oid, value Oid) error { } w.WriteInt32(4) - w.WriteInt32(int32(value)) + w.WriteUint32(uint32(value)) return nil } From 074bcd7139309f003b76859e91d8c8dd1576d740 Mon Sep 17 00:00:00 2001 From: Manni Wood Date: Sat, 3 Sep 2016 18:30:36 -0400 Subject: [PATCH 2/3] Adds docs for Oid type. --- messages.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/messages.go b/messages.go index c158d5d2..fec34dbb 100644 --- a/messages.go +++ b/messages.go @@ -53,6 +53,10 @@ func (s *startupMessage) Bytes() (buf []byte) { return buf } +// Oid (Object Identifier Type) is, according to https://www.postgresql.org/docs/current/static/datatype-oid.html, +// used internally by PostgreSQL as a primary key for various system tables. It is currently implemented +// as an unsigned four-byte integer. Its definition can be found in src/include/postgres_ext.h +// in the PostgreSQL sources. type Oid uint32 type FieldDescription struct { From 0c7277fe15359549b236292644f4ad7eff753544 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Mon, 5 Sep 2016 08:08:39 -0500 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffcc9594..a9d70ee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# Unreleased + +## Fixes + +* Oid underlying type changed to uint32, previously it was incorrectly int32 + # 2.9.0 (August 26, 2016) ## Fixes