From 812b43a4b301b2ff0a5bb6aac94d720197739470 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Fri, 30 Oct 2020 19:18:51 +0300 Subject: [PATCH] Pull request:* all: fix all staticcheck SA warnings Merge in DNS/adguard-home from 2238-fix-static-analisys-warnings to master Squashed commit of the following: commit 721ca6fa1cbfdfe9d414e6ed52fec4a64653fb52 Author: Eugene Burkov Date: Fri Oct 30 15:48:10 2020 +0300 * all: fix all staticcheck SA warnings Closes #2238. --- internal/dhcpd/dhcp_http.go | 2 +- internal/dhcpd/dhcpd_test.go | 3 +-- internal/dhcpd/v6.go | 1 + internal/dnsforward/dnsforward_test.go | 5 +++-- internal/home/tls.go | 14 ++------------ internal/querylog/decode_test.go | 4 ++-- internal/util/pprof.go | 14 ++++++-------- 7 files changed, 16 insertions(+), 27 deletions(-) diff --git a/internal/dhcpd/dhcp_http.go b/internal/dhcpd/dhcp_http.go index 447dcebf..71defe38 100644 --- a/internal/dhcpd/dhcp_http.go +++ b/internal/dhcpd/dhcp_http.go @@ -175,7 +175,7 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { v6conf.InterfaceName = newconfig.InterfaceName v6conf.notify = s.onNotify s6, err = v6Create(v6conf) - if s6 == nil { + if err != nil { httpError(r, w, http.StatusBadRequest, "Invalid DHCPv6 configuration: %s", err) return } diff --git a/internal/dhcpd/dhcpd_test.go b/internal/dhcpd/dhcpd_test.go index 9e4222cf..8fd795d1 100644 --- a/internal/dhcpd/dhcpd_test.go +++ b/internal/dhcpd/dhcpd_test.go @@ -83,7 +83,6 @@ func TestIsValidSubnetMask(t *testing.T) { func TestNormalizeLeases(t *testing.T) { dynLeases := []*Lease{} staticLeases := []*Lease{} - leases := []*Lease{} lease := &Lease{} lease.HWAddr = []byte{1, 2, 3, 4} @@ -100,7 +99,7 @@ func TestNormalizeLeases(t *testing.T) { lease.HWAddr = []byte{2, 2, 3, 4} staticLeases = append(staticLeases, lease) - leases = normalizeLeases(staticLeases, dynLeases) + leases := normalizeLeases(staticLeases, dynLeases) assert.True(t, len(leases) == 3) assert.True(t, bytes.Equal(leases[0].HWAddr, []byte{1, 2, 3, 4})) diff --git a/internal/dhcpd/v6.go b/internal/dhcpd/v6.go index 23c8b88b..e9ad0193 100644 --- a/internal/dhcpd/v6.go +++ b/internal/dhcpd/v6.go @@ -45,6 +45,7 @@ func ip6InRange(start net.IP, ip net.IP) bool { if len(start) != 16 { return false } + //lint:ignore SA1021 TODO(e.burkov): Ignore this for now, think about using masks. if !bytes.Equal(start[:15], ip[:15]) { return false } diff --git a/internal/dnsforward/dnsforward_test.go b/internal/dnsforward/dnsforward_test.go index 2b76602a..d81fec4f 100644 --- a/internal/dnsforward/dnsforward_test.go +++ b/internal/dnsforward/dnsforward_test.go @@ -815,12 +815,13 @@ func sendTestMessageAsync(t *testing.T, conn *dns.Conn, g *sync.WaitGroup) { req := createGoogleATestMessage() err := conn.WriteMsg(req) if err != nil { - t.Fatalf("cannot write message: %s", err) + panic(fmt.Sprintf("cannot write message: %s", err)) + } res, err := conn.ReadMsg() if err != nil { - t.Fatalf("cannot read response to message: %s", err) + panic(fmt.Sprintf("cannot read response to message: %s", err)) } assertGoogleAResponse(t, res) } diff --git a/internal/home/tls.go b/internal/home/tls.go index 0b9e8f90..d9ab356a 100644 --- a/internal/home/tls.go +++ b/internal/home/tls.go @@ -306,8 +306,7 @@ func verifyCertChain(data *tlsConfigStatus, certChain string, serverName string) log.Tracef("TLS: got certificate: %d bytes", len(certChain)) // now do a more extended validation - var certs []*pem.Block // PEM-encoded certificates - var skippedBytes []string // skipped bytes + var certs []*pem.Block // PEM-encoded certificates pemblock := []byte(certChain) for { @@ -318,10 +317,6 @@ func verifyCertChain(data *tlsConfigStatus, certChain string, serverName string) } if decoded.Type == "CERTIFICATE" { certs = append(certs, decoded) - } else { - // ignore "this result of append is never used" warning - // nolint - skippedBytes = append(skippedBytes, decoded.Type) } } @@ -387,8 +382,7 @@ func verifyCertChain(data *tlsConfigStatus, certChain string, serverName string) func validatePkey(data *tlsConfigStatus, pkey string) error { // now do a more extended validation - var key *pem.Block // PEM-encoded certificates - var skippedBytes []string // skipped bytes + var key *pem.Block // PEM-encoded certificates // go through all pem blocks, but take first valid pem block and drop the rest pemblock := []byte(pkey) @@ -401,10 +395,6 @@ func validatePkey(data *tlsConfigStatus, pkey string) error { if decoded.Type == "PRIVATE KEY" || strings.HasSuffix(decoded.Type, " PRIVATE KEY") { key = decoded break - } else { - // ignore "this result of append is never used" - // nolint - skippedBytes = append(skippedBytes, decoded.Type) } } diff --git a/internal/querylog/decode_test.go b/internal/querylog/decode_test.go index b5b3b7d9..2b12313a 100644 --- a/internal/querylog/decode_test.go +++ b/internal/querylog/decode_test.go @@ -15,7 +15,7 @@ func TestJSON(t *testing.T) { assert.Equal(t, "keystr", k) assert.Equal(t, "val", v) - k, v, jtype = readJSON(&s) + k, _, jtype = readJSON(&s) assert.Equal(t, jtype, int32(jsonTObj)) assert.Equal(t, "obj", k) @@ -29,6 +29,6 @@ func TestJSON(t *testing.T) { assert.Equal(t, "keyint", k) assert.Equal(t, "123456", v) - k, v, jtype = readJSON(&s) + _, _, jtype = readJSON(&s) assert.True(t, jtype == jsonTErr) } diff --git a/internal/util/pprof.go b/internal/util/pprof.go index 031e6c78..9a012d3d 100644 --- a/internal/util/pprof.go +++ b/internal/util/pprof.go @@ -64,6 +64,7 @@ package util import ( "bufio" "bytes" + "context" "fmt" "html/template" "io" @@ -94,14 +95,11 @@ func PProfRegisterWebHandlers(mux *http.ServeMux) { func Cmdline(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("Content-Type", "text/plain; charset=utf-8") - fmt.Fprintf(w, strings.Join(os.Args, "\x00")) + fmt.Fprint(w, strings.Join(os.Args, "\x00")) } -func sleep(w http.ResponseWriter, d time.Duration) { - var clientGone <-chan bool - if cn, ok := w.(http.CloseNotifier); ok { - clientGone = cn.CloseNotify() - } +func sleep(ctx context.Context, d time.Duration) { + clientGone := ctx.Done() select { case <-time.After(d): case <-clientGone: @@ -146,7 +144,7 @@ func Profile(w http.ResponseWriter, r *http.Request) { fmt.Sprintf("Could not enable CPU profiling: %s", err)) return } - sleep(w, time.Duration(sec)*time.Second) + sleep(r.Context(), time.Duration(sec)*time.Second) pprof.StopCPUProfile() } @@ -175,7 +173,7 @@ func Trace(w http.ResponseWriter, r *http.Request) { fmt.Sprintf("Could not enable tracing: %s", err)) return } - sleep(w, time.Duration(sec*float64(time.Second))) + sleep(r.Context(), time.Duration(sec*float64(time.Second))) trace.Stop() }