mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
simplify NewSize calls
This commit is contained in:
parent
0c213ccd20
commit
1cbfeea0cd
@ -51,12 +51,7 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var b *buf.Buffer
|
b := buf.NewSize(uint32(size))
|
||||||
if size <= buf.Size {
|
|
||||||
b = buf.New()
|
|
||||||
} else {
|
|
||||||
b = buf.NewSize(uint32(size))
|
|
||||||
}
|
|
||||||
if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, int(size))); err != nil {
|
if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, int(size))); err != nil {
|
||||||
b.Release()
|
b.Release()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -124,12 +124,7 @@ func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
var b *buf.Buffer
|
b := buf.NewSize(uint32(size))
|
||||||
if size <= buf.Size {
|
|
||||||
b = buf.New()
|
|
||||||
} else {
|
|
||||||
b = buf.NewSize(uint32(size))
|
|
||||||
}
|
|
||||||
if err := b.Reset(buf.ReadFullFrom(r.reader, size)); err != nil {
|
if err := b.Reset(buf.ReadFullFrom(r.reader, size)); err != nil {
|
||||||
b.Release()
|
b.Release()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -70,22 +70,14 @@ func NewChunkReader(reader io.Reader, auth *Authenticator) *ChunkReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *ChunkReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
func (v *ChunkReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
||||||
buffer := buf.New()
|
size, err := serial.ReadUint16(v.reader)
|
||||||
if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, 2)); err != nil {
|
if err != nil {
|
||||||
buffer.Release()
|
return nil, newError("failed to read size")
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// There is a potential buffer overflow here. Large buffer is 64K bytes,
|
|
||||||
// while uin16 + 10 will be more than that
|
|
||||||
length := serial.BytesToUint16(buffer.BytesTo(2)) + AuthSize
|
|
||||||
if length > buf.Size {
|
|
||||||
// Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer.
|
|
||||||
buffer.Release()
|
|
||||||
buffer = buf.NewSize(uint32(length) + 128)
|
|
||||||
}
|
}
|
||||||
|
size += AuthSize
|
||||||
|
|
||||||
buffer.Clear()
|
buffer := buf.NewSize(uint32(size))
|
||||||
if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, int(length))); err != nil {
|
if err := buffer.AppendSupplier(buf.ReadFullFrom(v.reader, int(size))); err != nil {
|
||||||
buffer.Release()
|
buffer.Release()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user