mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-18 07:17:32 -05:00
prevent empty read in Reader
This commit is contained in:
parent
fc7da93d06
commit
babd0107cf
@ -23,6 +23,7 @@ const xlSize = 128 * 1024
|
|||||||
|
|
||||||
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
|
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
|
||||||
b := New()
|
b := New()
|
||||||
|
for {
|
||||||
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)
|
||||||
@ -30,9 +31,12 @@ func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
|
|||||||
if !b.IsEmpty() {
|
if !b.IsEmpty() {
|
||||||
return NewMultiBufferValue(b), nil
|
return NewMultiBufferValue(b), nil
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
b.Release()
|
b.Release()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ReadMultiBuffer implements Reader.
|
// ReadMultiBuffer implements Reader.
|
||||||
func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
|
func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
|
||||||
@ -40,6 +44,7 @@ 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,10 +58,13 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
|
|||||||
}
|
}
|
||||||
return mb, nil
|
return mb, nil
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
freeBytes(r.buffer)
|
freeBytes(r.buffer)
|
||||||
r.buffer = nil
|
r.buffer = nil
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// BufferedReader is a Reader that keeps its internal buffer.
|
// BufferedReader is a Reader that keeps its internal buffer.
|
||||||
type BufferedReader struct {
|
type BufferedReader struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user