From f5d76c8eb0dabc081a55261c22b8c27d05848e03 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 7 Nov 2017 14:40:51 +0100 Subject: [PATCH] fix lint warnings --- common/crypto/auth.go | 7 ++++++- common/crypto/chunk.go | 19 +++++++++++-------- testing/scenarios/vmess_test.go | 4 +++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/common/crypto/auth.go b/common/crypto/auth.go index f9fccd24..4c619cdb 100644 --- a/common/crypto/auth.go +++ b/common/crypto/auth.go @@ -93,7 +93,12 @@ func (r *AuthenticationReader) readSize() error { sizeBytes := r.sizeParser.SizeBytes() if r.buffer.Len() < sizeBytes { - common.Must(r.buffer.Reset(buf.ReadFrom(r.buffer))) + if r.buffer.IsEmpty() { + r.buffer.Clear() + } else { + common.Must(r.buffer.Reset(buf.ReadFrom(r.buffer))) + } + delta := sizeBytes - r.buffer.Len() if err := r.buffer.AppendSupplier(buf.ReadAtLeastFrom(r.reader, delta)); err != nil { return err diff --git a/common/crypto/chunk.go b/common/crypto/chunk.go index 5b44640c..b3368c10 100644 --- a/common/crypto/chunk.go +++ b/common/crypto/chunk.go @@ -3,6 +3,7 @@ package crypto import ( "io" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" ) @@ -70,7 +71,7 @@ func (r *ChunkStreamReader) readSize() (uint16, error) { return 0, err } } - r.leftOver.Read(r.buffer) + common.Must2(r.leftOver.Read(r.buffer)) return r.sizeDecoder.Decode(r.buffer) } @@ -93,21 +94,23 @@ func (r *ChunkStreamReader) Read() (buf.MultiBuffer, error) { } } - if size >= r.leftOver.Len() { + leftOverLen := r.leftOver.Len() + if size >= leftOverLen { mb := r.leftOver - r.leftOverSize = size - r.leftOver.Len() + r.leftOverSize = size - leftOverLen r.leftOver = nil return mb, nil } mb := r.leftOver.SliceBySize(size) - if mb.Len() != size { + mbLen := mb.Len() + if mbLen != size { b := buf.New() - b.AppendSupplier(buf.ReadFullFrom(&r.leftOver, size-mb.Len())) + common.Must(b.Reset(buf.ReadFullFrom(&r.leftOver, size-mbLen))) mb.Append(b) } - r.leftOverSize = 0 + return mb, nil } @@ -131,10 +134,10 @@ func (w *ChunkStreamWriter) Write(mb buf.MultiBuffer) error { slice := mb.SliceBySize(sliceSize) b := buf.New() - b.AppendSupplier(func(buffer []byte) (int, error) { + common.Must(b.AppendSupplier(func(buffer []byte) (int, error) { w.sizeEncoder.Encode(uint16(slice.Len()), buffer[:0]) return w.sizeEncoder.SizeBytes(), nil - }) + })) mb2Write.Append(b) mb2Write.AppendMulti(slice) diff --git a/testing/scenarios/vmess_test.go b/testing/scenarios/vmess_test.go index 0a10669e..04b04356 100644 --- a/testing/scenarios/vmess_test.go +++ b/testing/scenarios/vmess_test.go @@ -637,6 +637,8 @@ func TestVMessNone(t *testing.T) { wg.Add(10) for i := 0; i < 10; i++ { go func() { + defer wg.Done() + conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), @@ -651,9 +653,9 @@ func TestVMessNone(t *testing.T) { assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 1024*1024) + assert(response, Equals, xor(payload)) assert(conn.Close(), IsNil) - wg.Done() }() } wg.Wait()