Pull request:* all: fix all staticcheck simplification and unused warnings

Merge in DNS/adguard-home from 2270-fix-s-u-warnings to master

Squashed commit of the following:

commit 03e0f78bd471057007c2d4042ee26eda2bbc9b29
Merge: 50dc3ef5c 7e16fda57
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Nov 6 11:54:09 2020 +0300

    Merge branch 'master' into 2270-fix-s-u-warnings

commit 50dc3ef5c44a5fdc941794c26784b0c44d7b5aa0
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Nov 5 19:48:54 2020 +0300

    * all: improve code quality

commit d6d804f759ce3e47154a389b427550e72c4b9090
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Nov 5 18:03:35 2020 +0300

    * all: fix all staticcheck simplification and unused warnings

    Closes #2270.
This commit is contained in:
Eugene Burkov
2020-11-06 12:15:08 +03:00
parent 7e16fda57b
commit 3e1f922252
24 changed files with 159 additions and 210 deletions

View File

@@ -27,14 +27,12 @@ type session struct {
expire uint32 // expiration time (in seconds)
}
/*
expire byte[4]
name_len byte[2]
name byte[]
*/
func (s *session) serialize() []byte {
var data []byte
data = make([]byte, 4+2+len(s.userName))
const (
expireLen = 4
nameLen = 2
)
data := make([]byte, expireLen+nameLen+len(s.userName))
binary.BigEndian.PutUint32(data[0:4], s.expire)
binary.BigEndian.PutUint16(data[4:6], uint16(len(s.userName)))
copy(data[6:], []byte(s.userName))
@@ -474,7 +472,7 @@ func (a *Auth) UserAdd(u *User, password string) {
}
// UserFind - find a user
func (a *Auth) UserFind(login string, password string) User {
func (a *Auth) UserFind(login, password string) User {
a.lock.Lock()
defer a.lock.Unlock()
for _, u := range a.users {

View File

@@ -16,7 +16,6 @@ import (
"time"
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/log"
)
@@ -548,8 +547,10 @@ func (f *Filtering) updateIntl(filter *filter) (bool, error) {
total += n
if htmlTest {
// gather full buffer firstChunk and perform its data tests
num := util.MinInt(n, len(firstChunk)-firstChunkLen)
num := len(firstChunk) - firstChunkLen
if n < num {
num = n
}
copied := copy(firstChunk[firstChunkLen:], buf[:num])
firstChunkLen += copied
@@ -559,8 +560,7 @@ func (f *Filtering) updateIntl(filter *filter) (bool, error) {
}
s := strings.ToLower(string(firstChunk))
if strings.Index(s, "<html") >= 0 ||
strings.Index(s, "<!doctype") >= 0 {
if strings.Contains(s, "<html") || strings.Contains(s, "<!doctype") {
return false, fmt.Errorf("data is HTML, not plain text")
}

View File

@@ -96,7 +96,7 @@ func (c *homeContext) getDataDir() string {
var Context homeContext
// Main is the entry point
func Main(version string, channel string, armVer string) {
func Main(version, channel, armVer string) {
// Init update-related global variables
versionString = version
updateChannel = channel
@@ -616,11 +616,7 @@ func detectFirstRun() bool {
configfile = filepath.Join(Context.workDir, Context.configFilename)
}
_, err := os.Stat(configfile)
if !os.IsNotExist(err) {
// do nothing, file exists
return false
}
return true
return os.IsNotExist(err)
}
// Connect to a remote server resolving hostname using our own DNS server

View File

@@ -58,9 +58,9 @@ func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
log.Printf("config.Language is %s", config.Language)
_, err := fmt.Fprintf(w, "%s\n", config.Language)
if err != nil {
errorText := fmt.Sprintf("Unable to write response json: %s", err)
log.Println(errorText)
http.Error(w, errorText, http.StatusInternalServerError)
msg := fmt.Sprintf("Unable to write response json: %s", err)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
}
@@ -68,23 +68,23 @@ func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
errorText := fmt.Sprintf("failed to read request body: %s", err)
log.Println(errorText)
http.Error(w, errorText, http.StatusBadRequest)
msg := fmt.Sprintf("failed to read request body: %s", err)
log.Println(msg)
http.Error(w, msg, http.StatusBadRequest)
return
}
language := strings.TrimSpace(string(body))
if language == "" {
errorText := fmt.Sprintf("empty language specified")
log.Println(errorText)
http.Error(w, errorText, http.StatusBadRequest)
msg := "empty language specified"
log.Println(msg)
http.Error(w, msg, http.StatusBadRequest)
return
}
if !isLanguageAllowed(language) {
errorText := fmt.Sprintf("unknown language specified: %s", language)
log.Println(errorText)
http.Error(w, errorText, http.StatusBadRequest)
msg := fmt.Sprintf("unknown language specified: %s", language)
log.Println(msg)
http.Error(w, msg, http.StatusBadRequest)
return
}

View File

@@ -32,13 +32,9 @@ func memoryUsage(args options) {
// that the OS could reclaim the free memory
go func() {
ticker := time.NewTicker(5 * time.Minute)
for {
select {
case t := <-ticker.C:
t.Second()
log.Debug("Free OS memory")
debug.FreeOSMemory()
}
for range ticker.C {
log.Debug("free os memory")
debug.FreeOSMemory()
}
}()
}

View File

@@ -116,8 +116,7 @@ func (r *RDNS) resolve(ip string) string {
// Add the hostname:IP pair to "Clients" array
func (r *RDNS) workerLoop() {
for {
var ip string
ip = <-r.ipChannel
ip := <-r.ipChannel
host := r.resolve(ip)
if len(host) == 0 {

View File

@@ -203,7 +203,7 @@ func handleServiceInstallCommand(s service.Service) {
log.Fatal(err)
}
if util.IsOpenWrt() {
if util.IsOpenWRT() {
// On OpenWrt it is important to run enable after the service installation
// Otherwise, the service won't start on the system startup
_, err := runInitdCommand("enable")
@@ -230,7 +230,7 @@ Click on the link below and follow the Installation Wizard steps to finish setup
// handleServiceStatusCommand handles service "uninstall" command
func handleServiceUninstallCommand(s service.Service) {
if util.IsOpenWrt() {
if util.IsOpenWRT() {
// On OpenWrt it is important to run disable command first
// as it will remove the symlink
_, err := runInitdCommand("disable")
@@ -272,14 +272,14 @@ func configureService(c *service.Config) {
// Redirect StdErr & StdOut to files.
c.Option["LogOutput"] = true
// Use modified service file templates
// Use modified service file templates.
c.Option["SystemdScript"] = systemdScript
c.Option["SysvScript"] = sysvScript
// On OpenWrt we're using a different type of sysvScript
if util.IsOpenWrt() {
// On OpenWrt we're using a different type of sysvScript.
if util.IsOpenWRT() {
c.Option["SysvScript"] = openWrtScript
} else if util.IsFreeBSD() {
} else if runtime.GOOS == "freebsd" {
c.Option["SysvScript"] = freeBSDScript
}
}
@@ -502,6 +502,7 @@ status() {
fi
}
`
const freeBSDScript = `#!/bin/sh
# PROVIDE: {{.Name}}
# REQUIRE: networking

View File

@@ -305,7 +305,7 @@ func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
}
}
func verifyCertChain(data *tlsConfigStatus, certChain string, serverName string) error {
func verifyCertChain(data *tlsConfigStatus, certChain, serverName string) error {
log.Tracef("TLS: got certificate: %d bytes", len(certChain))
// now do a more extended validation
@@ -335,7 +335,7 @@ func verifyCertChain(data *tlsConfigStatus, certChain string, serverName string)
}
if len(parsedCerts) == 0 {
data.WarningValidation = fmt.Sprintf("You have specified an empty certificate")
data.WarningValidation = "You have specified an empty certificate"
return errors.New(data.WarningValidation)
}

View File

@@ -116,7 +116,7 @@ func whoisParse(data string) map[string]string {
}
// Send request to a server and receive the response
func (w *Whois) query(target string, serverAddr string) (string, error) {
func (w *Whois) query(target, serverAddr string) (string, error) {
addr, _, _ := net.SplitHostPort(serverAddr)
if addr == "whois.arin.net" {
target = "n + " + target
@@ -224,8 +224,7 @@ func (w *Whois) Begin(ip string) {
// Get IP address from channel; get WHOIS info; associate info with a client
func (w *Whois) workerLoop() {
for {
var ip string
ip = <-w.ipChan
ip := <-w.ipChan
info := w.process(ip)
if len(info) == 0 {