diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 2aa60e6aa85a..232620c394b4 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -258,6 +258,9 @@ func (b *Buffer) IsFull() bool { func (b *Buffer) Write(data []byte) (int, error) { nBytes := copy(b.v[b.end:], data) b.end += int32(nBytes) + if nBytes != len(data) { + return nBytes, errors.New("incomplete write") + } return nBytes, nil } @@ -306,10 +309,11 @@ func (b *Buffer) Read(data []byte) (int, error) { nBytes := copy(data, b.v[b.start:b.end]) if int32(nBytes) == b.Len() { b.Clear() + return nBytes, nil } else { b.start += int32(nBytes) + return nBytes, errors.New("incomplete read") } - return nBytes, nil } // ReadFrom implements io.ReaderFrom. diff --git a/common/buf/multi_buffer.go b/common/buf/multi_buffer.go index f3b17aef4590..02449c5e1135 100644 --- a/common/buf/multi_buffer.go +++ b/common/buf/multi_buffer.go @@ -141,12 +141,22 @@ func Compact(mb MultiBuffer) MultiBuffer { mb2 := make(MultiBuffer, 0, len(mb)) last := mb[0] + if last.start != 0 { + b := New() + common.Must2(b.ReadFrom(last)) + last = b + } for i := 1; i < len(mb); i++ { curr := mb[i] if last.Len()+curr.Len() > Size { mb2 = append(mb2, last) last = curr + if last.start != 0 { + b := New() + common.Must2(b.ReadFrom(last)) + last = b + } } else { common.Must2(last.ReadFrom(curr)) curr.Release()