From 1de50450031878c1f2ae6e54f493e9f742028ff1 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Wed, 8 Apr 2020 14:59:09 +0100 Subject: [PATCH] Use errors from io --- internal/multiplex/datagramBuffer.go | 3 +-- internal/multiplex/obfs.go | 3 ++- internal/multiplex/recvBuffer.go | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/multiplex/datagramBuffer.go b/internal/multiplex/datagramBuffer.go index eebbf48..7232061 100644 --- a/internal/multiplex/datagramBuffer.go +++ b/internal/multiplex/datagramBuffer.go @@ -3,7 +3,6 @@ package multiplex import ( - "errors" "io" "sync" "sync/atomic" @@ -43,7 +42,7 @@ func (d *datagramBuffer) Read(target []byte) (int, error) { } data := d.buf[0] if len(target) < len(data) { - return 0, errors.New("buffer is too small") + return 0, io.ErrShortBuffer } d.buf = d.buf[1:] copy(target, data) diff --git a/internal/multiplex/obfs.go b/internal/multiplex/obfs.go index dd60acc..edaf381 100644 --- a/internal/multiplex/obfs.go +++ b/internal/multiplex/obfs.go @@ -9,6 +9,7 @@ import ( "github.com/cbeuw/Cloak/internal/util" "golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/salsa20" + "io" ) type Obfser func(*Frame, []byte) (int, error) @@ -57,7 +58,7 @@ func MakeObfs(salsaKey [32]byte, payloadCipher cipher.AEAD, hasRecordLayer bool) // usefulLen is the amount of bytes that will be eventually sent off usefulLen := rlLen + HEADER_LEN + len(f.Payload) + int(extraLen) if len(buf) < usefulLen { - return 0, errors.New("buffer is too small") + return 0, io.ErrShortBuffer } // we do as much in-place as possible to save allocation diff --git a/internal/multiplex/recvBuffer.go b/internal/multiplex/recvBuffer.go index db5c844..babc274 100644 --- a/internal/multiplex/recvBuffer.go +++ b/internal/multiplex/recvBuffer.go @@ -3,6 +3,7 @@ package multiplex import "io" type recvBuffer interface { + // Read calls' err must be nil | io.EOF | io.ErrShortBuffer io.ReadCloser Write(Frame) (toBeClosed bool, err error) }