From c67a9331a4f7c25df943ff821c9c6ed8013df869 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 1 Aug 2018 13:19:54 +0200 Subject: gometalinter: clean up some errcheck warnings Check some errors where relevant. Unfortunately enabling errcheck does not look feasible, too many warnings. Update #538 --- pkg/db/db.go | 8 ++++---- pkg/host/host_linux.go | 17 +++++++++++++---- pkg/rpctype/rpc.go | 24 +++++++++++++++--------- 3 files changed, 32 insertions(+), 17 deletions(-) (limited to 'pkg') diff --git a/pkg/db/db.go b/pkg/db/db.go index 4d48e1229..5c2755860 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -47,7 +47,9 @@ func Open(filename string) (*DB, error) { db.Version, db.Records, db.uncompacted = deserializeDB(bufio.NewReader(f)) f.Close() if len(db.Records) == 0 || db.uncompacted/10*9 > len(db.Records) { - db.compact() + if err := db.compact(); err != nil { + return nil, err + } } return db, nil } @@ -75,8 +77,7 @@ func (db *DB) Delete(key string) { func (db *DB) Flush() error { if db.uncompacted/10*9 > len(db.Records) { - db.compact() - return nil + return db.compact() } if db.pending == nil { return nil @@ -168,7 +169,6 @@ func serializeRecord(w *bytes.Buffer, key string, val []byte, seq uint64) { if _, err := fw.Write(val); err != nil { panic(err) } - fw.Flush() fw.Close() binary.Write(bytes.NewBuffer(w.Bytes()[lenPos:lenPos:lenPos+8]), binary.LittleEndian, uint32(len(w.Bytes())-startPos)) } diff --git a/pkg/host/host_linux.go b/pkg/host/host_linux.go index 38b2461f1..6d7c9c516 100644 --- a/pkg/host/host_linux.go +++ b/pkg/host/host_linux.go @@ -337,8 +337,8 @@ func checkCoverage() string { return "" } -func checkComparisons() string { - if reason := checkDebugFS(); reason != "" { +func checkComparisons() (reason string) { + if reason = checkDebugFS(); reason != "" { return reason } // TODO(dvyukov): this should run under target arch. @@ -365,7 +365,11 @@ func checkComparisons() string { if err != nil { return fmt.Sprintf("KCOV mmap failed: %v", err) } - defer syscall.Munmap(mem) + defer func() { + if err := syscall.Munmap(mem); err != nil { + reason = fmt.Sprintf("munmap failed: %v", err) + } + }() _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), linux.KCOV_ENABLE, linux.KCOV_TRACE_CMP) if errno != 0 { @@ -374,7 +378,12 @@ func checkComparisons() string { } return fmt.Sprintf("ioctl(KCOV_TRACE_CMP) failed: %v", errno) } - defer syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), linux.KCOV_DISABLE, 0) + defer func() { + _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), linux.KCOV_DISABLE, 0) + if errno != 0 { + reason = fmt.Sprintf("ioctl(KCOV_DISABLE) failed: %v", errno) + } + }() return "" } diff --git a/pkg/rpctype/rpc.go b/pkg/rpctype/rpc.go index 3838855a4..417a4f7b8 100644 --- a/pkg/rpctype/rpc.go +++ b/pkg/rpctype/rpc.go @@ -26,7 +26,9 @@ func NewRPCServer(addr string, receiver interface{}) (*RPCServer, error) { return nil, fmt.Errorf("failed to listen on %v: %v", addr, err) } s := rpc.NewServer() - s.Register(receiver) + if err := s.Register(receiver); err != nil { + return nil, err + } serv := &RPCServer{ ln: ln, s: s, @@ -41,8 +43,7 @@ func (serv *RPCServer) Serve() { log.Logf(0, "failed to accept an rpc connection: %v", err) continue } - conn.(*net.TCPConn).SetKeepAlive(true) - conn.(*net.TCPConn).SetKeepAlivePeriod(10 * time.Second) + setupKeepAlive(conn, 10*time.Second) go serv.s.ServeConn(newFlateConn(conn)) } } @@ -66,8 +67,7 @@ func Dial(addr string) (net.Conn, error) { if conn, err = net.DialTimeout("tcp", addr, 60*time.Second); err != nil { return nil, err } - conn.(*net.TCPConn).SetKeepAlive(true) - conn.(*net.TCPConn).SetKeepAlivePeriod(time.Minute) + setupKeepAlive(conn, time.Minute) return conn, nil } @@ -84,10 +84,11 @@ func NewRPCClient(addr string) (*RPCClient, error) { } func (cli *RPCClient) Call(method string, args, reply interface{}) error { - cli.conn.SetDeadline(time.Now().Add(5 * 60 * time.Second)) - err := cli.c.Call(method, args, reply) - cli.conn.SetDeadline(time.Time{}) - return err + if err := cli.conn.SetDeadline(time.Now().Add(5 * 60 * time.Second)); err != nil { + return err + } + defer cli.conn.SetDeadline(time.Time{}) + return cli.c.Call(method, args, reply) } func (cli *RPCClient) Close() { @@ -103,6 +104,11 @@ func RPCCall(addr, method string, args, reply interface{}) error { return c.Call(method, args, reply) } +func setupKeepAlive(conn net.Conn, keepAlive time.Duration) { + conn.(*net.TCPConn).SetKeepAlive(true) + conn.(*net.TCPConn).SetKeepAlivePeriod(keepAlive) +} + // flateConn wraps net.Conn in flate.Reader/Writer for compressed traffic. type flateConn struct { r io.ReadCloser -- cgit mrf-deployment