Skip to content

Commit

Permalink
Don't panic when receiving zero bytes with "slice bounds out of range"
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalif authored and jackc committed Jul 1, 2022
1 parent 175856f commit fd427c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pgproto3

import (
"encoding/binary"
"errors"
"fmt"
"io"
)
Expand Down Expand Up @@ -114,6 +115,9 @@ func (b *Backend) Receive() (FrontendMessage, error) {
b.msgType = header[0]
b.bodyLen = int(binary.BigEndian.Uint32(header[1:])) - 4
b.partialMsg = true
if b.bodyLen < 0 {
return nil, errors.New("invalid message with negative body length received")
}
}

var msg FrontendMessage
Expand Down
3 changes: 3 additions & 0 deletions frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (f *Frontend) Receive() (BackendMessage, error) {
f.msgType = header[0]
f.bodyLen = int(binary.BigEndian.Uint32(header[1:])) - 4
f.partialMsg = true
if f.bodyLen < 0 {
return nil, errors.New("invalid message with negative body length received")
}
}

msgBody, err := f.cr.Next(f.bodyLen)
Expand Down

0 comments on commit fd427c0

Please sign in to comment.