1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-03 07:56:42 -05:00

refine handling for empty read

This commit is contained in:
Darien Raymond 2018-03-29 18:15:50 +02:00
parent babd0107cf
commit bb46a96f04
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -23,7 +23,7 @@ const xlSize = 128 * 1024
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) { func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
b := New() b := New()
for { for i := 0; i < 64; i++ {
err := b.Reset(ReadFrom(r.Reader)) err := b.Reset(ReadFrom(r.Reader))
if b.IsFull() { if b.IsFull() {
r.buffer = newBytes(Size + 1) r.buffer = newBytes(Size + 1)
@ -36,6 +36,13 @@ func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
return nil, err return nil, err
} }
} }
return nil, newError("Reader returns too many empty payloads.")
}
func (r *BytesToBufferReader) freeBuffer() {
freeBytes(r.buffer)
r.buffer = nil
} }
// ReadMultiBuffer implements Reader. // ReadMultiBuffer implements Reader.
@ -44,7 +51,6 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
return r.readSmall() return r.readSmall()
} }
for {
nBytes, err := r.Reader.Read(r.buffer) nBytes, err := r.Reader.Read(r.buffer)
if nBytes > 0 { if nBytes > 0 {
mb := NewMultiBufferCap(nBytes/Size + 1) mb := NewMultiBufferCap(nBytes/Size + 1)
@ -53,17 +59,19 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
freeBytes(r.buffer) freeBytes(r.buffer)
r.buffer = newBytes(uint32(nBytes) + 1) r.buffer = newBytes(uint32(nBytes) + 1)
} else if nBytes < Size { } else if nBytes < Size {
freeBytes(r.buffer) r.freeBuffer()
r.buffer = nil
} }
return mb, nil return mb, nil
} }
r.freeBuffer()
if err != nil { if err != nil {
freeBytes(r.buffer)
r.buffer = nil
return nil, err return nil, err
} }
}
// Read() returns empty payload and nil err. We don't expect this to happen, but just in case.
return r.readSmall()
} }
// BufferedReader is a Reader that keeps its internal buffer. // BufferedReader is a Reader that keeps its internal buffer.