diff --git a/common/buf/multi_buffer.go b/common/buf/multi_buffer.go index 6f92717c..a93aa119 100644 --- a/common/buf/multi_buffer.go +++ b/common/buf/multi_buffer.go @@ -118,7 +118,7 @@ func (mb *MultiBuffer) ReadFrom(reader io.Reader) (int64, error) { // Read implements io.Reader. func (mb *MultiBuffer) Read(b []byte) (int, error) { - if mb.Len() == 0 { + if mb.IsEmpty() { return 0, io.EOF } endIndex := len(*mb) diff --git a/common/buf/reader.go b/common/buf/reader.go index 5f3ac868..cd9d93a8 100644 --- a/common/buf/reader.go +++ b/common/buf/reader.go @@ -97,8 +97,9 @@ func (r *BufferedReader) ReadByte() (byte, error) { // Read implements io.Reader. It reads from internal buffer first (if available) and then reads from the underlying reader. func (r *BufferedReader) Read(b []byte) (int, error) { - if r.Buffer != nil { - nBytes, _ := r.Buffer.Read(b) + if !r.Buffer.IsEmpty() { + nBytes, err := r.Buffer.Read(b) + common.Must(err) if r.Buffer.IsEmpty() { r.Buffer.Release() r.Buffer = nil @@ -113,19 +114,21 @@ func (r *BufferedReader) Read(b []byte) (int, error) { } mb, err := r.Reader.ReadMultiBuffer() - if mb != nil { - nBytes, _ := mb.Read(b) - if !mb.IsEmpty() { - r.Buffer = mb - } - return nBytes, err + if err != nil { + return 0, err } - return 0, err + + nBytes, err := mb.Read(b) + common.Must(err) + if !mb.IsEmpty() { + r.Buffer = mb + } + return nBytes, err } // ReadMultiBuffer implements Reader. func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error) { - if r.Buffer != nil { + if !r.Buffer.IsEmpty() { mb := r.Buffer r.Buffer = nil return mb, nil @@ -136,7 +139,7 @@ func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error) { // ReadAtMost returns a MultiBuffer with at most size. func (r *BufferedReader) ReadAtMost(size int32) (MultiBuffer, error) { - if r.Buffer == nil { + if r.Buffer.IsEmpty() { mb, err := r.Reader.ReadMultiBuffer() if mb.IsEmpty() && err != nil { return nil, err diff --git a/transport/internet/kcp/receiving.go b/transport/internet/kcp/receiving.go index d6a0d063..3bf5ee75 100644 --- a/transport/internet/kcp/receiving.go +++ b/transport/internet/kcp/receiving.go @@ -3,6 +3,7 @@ package kcp import ( "sync" + "v2ray.com/core/common" "v2ray.com/core/common/buf" ) @@ -204,7 +205,8 @@ func (w *ReceivingWorker) ReadMultiBuffer() buf.MultiBuffer { func (w *ReceivingWorker) Read(b []byte) int { mb := w.ReadMultiBuffer() - nBytes, _ := mb.Read(b) + nBytes, err := mb.Read(b) + common.Must(err) if !mb.IsEmpty() { w.leftOver = mb }