Do not create empty slices in Bind.Decode

This commit is contained in:
Jack Christensen 2017-05-19 15:50:27 -05:00
parent dbcfa46d8e
commit 3080d0ee4d
2 changed files with 33 additions and 29 deletions

View File

@ -209,8 +209,6 @@ func PgxInitSteps() []Step {
}), }),
SendMessage(&pgproto3.ReadyForQuery{TxStatus: 'I'}), SendMessage(&pgproto3.ReadyForQuery{TxStatus: 'I'}),
ExpectMessage(&pgproto3.Bind{ ExpectMessage(&pgproto3.Bind{
ParameterFormatCodes: []int16{},
Parameters: [][]byte{},
ResultFormatCodes: []int16{1, 1}, ResultFormatCodes: []int16{1, 1},
}), }),
ExpectMessage(&pgproto3.Execute{}), ExpectMessage(&pgproto3.Execute{}),

View File

@ -18,6 +18,8 @@ type Bind struct {
func (*Bind) Frontend() {} func (*Bind) Frontend() {}
func (dst *Bind) Decode(src []byte) error { func (dst *Bind) Decode(src []byte) error {
*dst = Bind{}
idx := bytes.IndexByte(src, 0) idx := bytes.IndexByte(src, 0)
if idx < 0 { if idx < 0 {
return &invalidMessageFormatErr{messageType: "Bind"} return &invalidMessageFormatErr{messageType: "Bind"}
@ -38,6 +40,7 @@ func (dst *Bind) Decode(src []byte) error {
parameterFormatCodeCount := int(binary.BigEndian.Uint16(src[rp:])) parameterFormatCodeCount := int(binary.BigEndian.Uint16(src[rp:]))
rp += 2 rp += 2
if parameterFormatCodeCount > 0 {
dst.ParameterFormatCodes = make([]int16, parameterFormatCodeCount) dst.ParameterFormatCodes = make([]int16, parameterFormatCodeCount)
if len(src[rp:]) < len(dst.ParameterFormatCodes)*2 { if len(src[rp:]) < len(dst.ParameterFormatCodes)*2 {
@ -47,6 +50,7 @@ func (dst *Bind) Decode(src []byte) error {
dst.ParameterFormatCodes[i] = int16(binary.BigEndian.Uint16(src[rp:])) dst.ParameterFormatCodes[i] = int16(binary.BigEndian.Uint16(src[rp:]))
rp += 2 rp += 2
} }
}
if len(src[rp:]) < 2 { if len(src[rp:]) < 2 {
return &invalidMessageFormatErr{messageType: "Bind"} return &invalidMessageFormatErr{messageType: "Bind"}
@ -54,6 +58,7 @@ func (dst *Bind) Decode(src []byte) error {
parameterCount := int(binary.BigEndian.Uint16(src[rp:])) parameterCount := int(binary.BigEndian.Uint16(src[rp:]))
rp += 2 rp += 2
if parameterCount > 0 {
dst.Parameters = make([][]byte, parameterCount) dst.Parameters = make([][]byte, parameterCount)
for i := 0; i < parameterCount; i++ { for i := 0; i < parameterCount; i++ {
@ -76,6 +81,7 @@ func (dst *Bind) Decode(src []byte) error {
dst.Parameters[i] = src[rp : rp+msgSize] dst.Parameters[i] = src[rp : rp+msgSize]
rp += msgSize rp += msgSize
} }
}
if len(src[rp:]) < 2 { if len(src[rp:]) < 2 {
return &invalidMessageFormatErr{messageType: "Bind"} return &invalidMessageFormatErr{messageType: "Bind"}