Unverified Commit b47fa43a authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #12 from libp2p/fix/read-partial

fix: never claim to read more than read
parents fd3a1c8f 345e83f4
......@@ -179,9 +179,13 @@ func (s *reader) Read(msg []byte) (int, error) {
return 0, io.ErrShortBuffer
}
_, err = io.ReadFull(s.R, msg[:length])
s.next = -1 // signal we've consumed this msg
return length, err
read, err := io.ReadFull(s.R, msg[:length])
if read < length {
s.next = length - read // we only partially consumed the message.
} else {
s.next = -1 // signal we've consumed this msg
}
return read, err
}
func (s *reader) ReadMsg() ([]byte, error) {
......@@ -203,9 +207,13 @@ func (s *reader) ReadMsg() ([]byte, error) {
}
msg := s.pool.Get(length)
_, err = io.ReadFull(s.R, msg)
s.next = -1 // signal we've consumed this msg
return msg, err
read, err := io.ReadFull(s.R, msg)
if read < length {
s.next = length - read // we only partially consumed the message.
} else {
s.next = -1 // signal we've consumed this msg
}
return msg[:read], err
}
func (s *reader) ReleaseMsg(msg []byte) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment