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 <e.burkov@adguard.com>
Date:   Fri Oct 30 15:48:10 2020 +0300

    * all: fix all staticcheck SA warnings

    Closes #2238.
This commit is contained in:
Eugene Burkov 2020-10-30 19:18:51 +03:00
parent ae8de95d89
commit 812b43a4b3
7 changed files with 16 additions and 27 deletions

View File

@ -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
}

View File

@ -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}))

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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()
}