From b94ccae4c9a6b3086c46d15d6253cb083c26fc3d Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 11 Mar 2017 20:12:47 -0600 Subject: [PATCH] Document that Decode* must not keep src - Also fix Bytea.DecodeBinary to not keep src. --- bytea.go | 5 ++++- pgtype.go | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bytea.go b/bytea.go index 9d2e20f3..a8ee55ae 100644 --- a/bytea.go +++ b/bytea.go @@ -106,7 +106,10 @@ func (dst *Bytea) DecodeBinary(src []byte) error { return nil } - *dst = Bytea{Bytes: src, Status: Present} + buf := make([]byte, len(src)) + copy(buf, src) + + *dst = Bytea{Bytes: buf, Status: Present} return nil } diff --git a/pgtype.go b/pgtype.go index 5a51172e..7b1470b7 100644 --- a/pgtype.go +++ b/pgtype.go @@ -80,10 +80,16 @@ type Value interface { } type BinaryDecoder interface { + // DecodeBinary decodes src into BinaryDecoder. If src is nil then the + // original SQL value is NULL. BinaryDecoder MUST not retain a reference to + // src. It MUST make a copy if it needs to retain the raw bytes. DecodeBinary(src []byte) error } type TextDecoder interface { + // DecodeText decodes src into TextDecoder. If src is nil then the original + // SQL value is NULL. TextDecoder MUST not retain a reference to src. It MUST + // make a copy if it needs to retain the raw bytes. DecodeText(src []byte) error }