Skip to content

Commit

Permalink
enable errcheck (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Aug 13, 2023
1 parent 916287c commit 0e8f34f
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 29 deletions.
37 changes: 35 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 11 additions & 6 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions endpoint_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/autoreconnector/auto_reconnector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
}()

Expand Down
15 changes: 11 additions & 4 deletions pkg/frame/v1frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions pkg/frame/v2frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions pkg/x25/x25.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 0e8f34f

Please sign in to comment.