From 0e8f34f79843ecc1301005a5026cab4a7b8bd269 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Sun, 13 Aug 2023 16:01:08 +0200 Subject: [PATCH] enable errcheck (#65) --- .golangci.yml | 37 ++++++++++++++++++-- channel.go | 17 +++++---- endpoint.go | 2 +- endpoint_server.go | 3 +- pkg/autoreconnector/auto_reconnector_test.go | 8 ++--- pkg/frame/v1frame.go | 15 +++++--- pkg/frame/v2frame.go | 9 ++--- pkg/x25/x25.go | 7 ++-- 8 files changed, 69 insertions(+), 29 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index dadbd3f6c..aca0060fb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -15,13 +15,46 @@ linters: - unconvert - wastedassign - whitespace - disable: - - errcheck issues: exclude-use-default: false linters-settings: + errcheck: + exclude-functions: + - io.Copy + - (io.Closer).Close + - (io.Writer).Write + - (hash.Hash).Write + - (net.Conn).Close + - (net.Conn).SetReadDeadline + - (net.Conn).SetWriteDeadline + - (*net.TCPConn).SetKeepAlive + - (*net.TCPConn).SetKeepAlivePeriod + - (*net.TCPConn).SetNoDelay + - (net.Listener).Close + - (net.PacketConn).Close + - (net.PacketConn).SetReadDeadline + - (net.PacketConn).SetWriteDeadline + - (net/http.ResponseWriter).Write + - (*net/http.Server).Serve + - (*net/http.Server).ServeTLS + - (*net/http.Server).Shutdown + - os.Chdir + - os.Mkdir + - os.MkdirAll + - os.Remove + - os.RemoveAll + - os.Setenv + - os.Unsetenv + - (*os.File).WriteString + - (*os.File).Close + - (github.com/datarhei/gosrt.Conn).Close + - (github.com/datarhei/gosrt.Conn).SetReadDeadline + - (github.com/datarhei/gosrt.Conn).SetWriteDeadline + - (*github.com/bluenviron/gortsplib/v3.Client).Close + - (*github.com/bluenviron/gortsplib/v3.Server).Close + govet: enable-all: true disable: diff --git a/channel.go b/channel.go index bf72ac268..9b430f173 100644 --- a/channel.go +++ b/channel.go @@ -8,10 +8,10 @@ import ( "github.com/bluenviron/gomavlib/v2/pkg/message" ) -func randomByte() byte { +func randomByte() (byte, error) { var buf [1]byte - rand.Read(buf[:]) - return buf[0] + _, err := rand.Read(buf[:]) + return buf[0], err } // Channel is a communication channel created by an Endpoint. @@ -32,6 +32,11 @@ type Channel struct { } func newChannel(n *Node, e Endpoint, label string, rwc io.ReadWriteCloser) (*Channel, error) { + linkID, err := randomByte() + if err != nil { + return nil, err + } + frw, err := frame.NewReadWriter(frame.ReadWriterConf{ ReadWriter: rwc, DialectRW: n.dialectRW, @@ -44,7 +49,7 @@ func newChannel(n *Node, e Endpoint, label string, rwc io.ReadWriteCloser) (*Cha return frame.V1 }(), OutComponentID: n.conf.OutComponentID, - OutSignatureLinkID: randomByte(), + OutSignatureLinkID: linkID, OutKey: n.conf.OutKey, }) if err != nil { @@ -124,10 +129,10 @@ func (ch *Channel) run() { for what := range ch.write { switch wh := what.(type) { case message.Message: - ch.frw.WriteMessage(wh) + ch.frw.WriteMessage(wh) //nolint:errcheck case frame.Frame: - ch.frw.WriteFrame(wh) + ch.frw.WriteFrame(wh) //nolint:errcheck } } }() diff --git a/endpoint.go b/endpoint.go index 0570aa6b4..832b244df 100644 --- a/endpoint.go +++ b/endpoint.go @@ -31,6 +31,6 @@ type endpointChannelSingle interface { // endpointChannelAccepter is an endpoint that provides multiple channels. type endpointChannelAccepter interface { Endpoint - close() error + close() accept() (string, io.ReadWriteCloser, error) } diff --git a/endpoint_server.go b/endpoint_server.go index 834c0b73c..85fd13754 100644 --- a/endpoint_server.go +++ b/endpoint_server.go @@ -108,10 +108,9 @@ func (t *endpointServer) Conf() EndpointConf { return t.conf } -func (t *endpointServer) close() error { +func (t *endpointServer) close() { close(t.terminate) t.listener.Close() - return nil } func (t *endpointServer) accept() (string, io.ReadWriteCloser, error) { diff --git a/pkg/autoreconnector/auto_reconnector_test.go b/pkg/autoreconnector/auto_reconnector_test.go index b6d1d49f1..b2efe4ffe 100644 --- a/pkg/autoreconnector/auto_reconnector_test.go +++ b/pkg/autoreconnector/auto_reconnector_test.go @@ -60,9 +60,9 @@ func TestCloseWhileWorking(t *testing.T) { b := make([]byte, 1) if ca == "read" { - conn.Read(b) + conn.Read(b) //nolint:errcheck } else { - conn.Write(b) + conn.Write(b) //nolint:errcheck } conn.Close() @@ -82,9 +82,9 @@ func TestCloseWhileWorking(t *testing.T) { p := make([]byte, 1) if ca == "read" { - a.Read(p) + a.Read(p) //nolint:errcheck } else { - a.Write(p) + a.Write(p) //nolint:errcheck } }() diff --git a/pkg/frame/v1frame.go b/pkg/frame/v1frame.go index 022d57bff..b187487fc 100644 --- a/pkg/frame/v1frame.go +++ b/pkg/frame/v1frame.go @@ -15,6 +15,15 @@ const ( V1MagicByte = 0xFE ) +func peekAndDiscard(br *bufio.Reader, size int) ([]byte, error) { + buf, err := br.Peek(size) + if err != nil { + return nil, err + } + br.Discard(size) //nolint:errcheck + return buf, nil +} + // V1Frame is a Mavlink V1 frame. type V1Frame struct { SequenceID byte @@ -63,11 +72,10 @@ func (f V1Frame) GenerateChecksum(crcExtra byte) uint16 { func (f *V1Frame) decode(br *bufio.Reader) error { // header - buf, err := br.Peek(5) + buf, err := peekAndDiscard(br, 5) if err != nil { return err } - br.Discard(5) msgLen := buf[0] f.SequenceID = buf[1] f.SystemID = buf[2] @@ -89,11 +97,10 @@ func (f *V1Frame) decode(br *bufio.Reader) error { } // checksum - buf, err = br.Peek(2) + buf, err = peekAndDiscard(br, 2) if err != nil { return err } - br.Discard(2) f.Checksum = binary.LittleEndian.Uint16(buf) return nil diff --git a/pkg/frame/v2frame.go b/pkg/frame/v2frame.go index 86a6dee44..7dfca85f2 100644 --- a/pkg/frame/v2frame.go +++ b/pkg/frame/v2frame.go @@ -149,11 +149,10 @@ func (f V2Frame) GenerateSignature(key *V2Key) *V2Signature { func (f *V2Frame) decode(br *bufio.Reader) error { // header - buf, err := br.Peek(9) + buf, err := peekAndDiscard(br, 9) if err != nil { return err } - br.Discard(9) msgLen := buf[0] f.IncompatibilityFlag = buf[1] f.CompatibilityFlag = buf[2] @@ -182,20 +181,18 @@ func (f *V2Frame) decode(br *bufio.Reader) error { } // checksum - buf, err = br.Peek(2) + buf, err = peekAndDiscard(br, 2) if err != nil { return err } - br.Discard(2) f.Checksum = binary.LittleEndian.Uint16(buf) // signature if f.IsSigned() { - buf, err := br.Peek(13) + buf, err := peekAndDiscard(br, 13) if err != nil { return err } - br.Discard(13) f.SignatureLinkID = buf[0] f.SignatureTimestamp = uint48Decode(buf[1:]) f.Signature = new(V2Signature) diff --git a/pkg/x25/x25.go b/pkg/x25/x25.go index 1f2f852c1..e4bad9ffd 100644 --- a/pkg/x25/x25.go +++ b/pkg/x25/x25.go @@ -31,18 +31,17 @@ func (x *X25) BlockSize() int { return 1 } -// Write adds more data to the running hash. -func (x *X25) Write(p []byte) (int, error) { +// Write adds more data to the hash. +func (x *X25) Write(p []byte) { for _, b := range p { tmp := uint16(b) ^ (x.crc & 0xFF) tmp ^= (tmp << 4) tmp &= 0xFF x.crc = (x.crc >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4) } - return len(p), nil } -// Sum16 returns the curren thash. +// Sum16 returns the current hash. func (x *X25) Sum16() uint16 { return x.crc }