From 2c9992e0ccc7b494892dd96e62678d5eff2ee6ba Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Thu, 11 Mar 2021 15:38:39 +0300 Subject: [PATCH] Pull request: all: rm var shadowing, vol. 1 Updates #2803. Squashed commit of the following: commit 15fa64ad4fb7561c4b0542245f99869685643bbd Author: Ainar Garipov Date: Thu Mar 11 14:55:28 2021 +0300 util: imp code commit c3b0563a44ccc98a5901df19174acdca8a350d62 Author: Ainar Garipov Date: Thu Mar 11 14:38:54 2021 +0300 all: rm var shadowing, vol. 1 --- internal/aghtest/os_windows.go | 2 +- internal/updater/updater.go | 9 ++++++--- internal/util/autohosts.go | 29 +++++++++++++++++------------ internal/util/helpers.go | 3 ++- internal/util/network.go | 3 ++- internal/util/tls.go | 9 +++++++-- 6 files changed, 35 insertions(+), 20 deletions(-) diff --git a/internal/aghtest/os_windows.go b/internal/aghtest/os_windows.go index e623d05e..a1c5ac33 100644 --- a/internal/aghtest/os_windows.go +++ b/internal/aghtest/os_windows.go @@ -44,7 +44,7 @@ func prepareTestDir(t *testing.T) (dir string) { t.Cleanup(func() { start := time.Now() for { - err := os.RemoveAll(dir) + err = os.RemoveAll(dir) if err == nil { break } diff --git a/internal/updater/updater.go b/internal/updater/updater.go index 73a2f7cb..4bc96fc3 100644 --- a/internal/updater/updater.go +++ b/internal/updater/updater.go @@ -337,7 +337,8 @@ func tarGzFileUnpack(tarfile, outdir string) ([]string, error) { var err2 error tarReader := tar.NewReader(gzReader) for { - header, err := tarReader.Next() + var header *tar.Header + header, err = tarReader.Next() if err == io.EOF { err2 = nil break @@ -377,7 +378,8 @@ func tarGzFileUnpack(tarfile, outdir string) ([]string, error) { continue } - f, err := os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode&0o777)) + var f io.WriteCloser + f, err = os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode&0o777)) if err != nil { err2 = fmt.Errorf("os.OpenFile(%s): %w", outputName, err) break @@ -449,7 +451,8 @@ func zipFileUnpack(zipfile, outdir string) ([]string, error) { continue } - f, err := os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode()) + var f io.WriteCloser + f, err = os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode()) if err != nil { err2 = fmt.Errorf("os.OpenFile(): %w", err) break diff --git a/internal/util/autohosts.go b/internal/util/autohosts.go index e4cea58c..fe870566 100644 --- a/internal/util/autohosts.go +++ b/internal/util/autohosts.go @@ -10,10 +10,9 @@ import ( "strings" "sync" - "github.com/miekg/dns" - "github.com/AdguardTeam/golibs/log" "github.com/fsnotify/fsnotify" + "github.com/miekg/dns" ) type onChangedT func() @@ -228,15 +227,17 @@ func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string][]string r := bufio.NewReader(f) log.Debug("AutoHosts: loading hosts from file %s", fn) - finish := false - for !finish { - line, err := r.ReadString('\n') + for done := false; !done; { + var line string + line, err = r.ReadString('\n') if err == io.EOF { - finish = true + done = true } else if err != nil { log.Error("AutoHosts: %s", err) + return } + line = strings.TrimSpace(line) if len(line) == 0 || line[0] == '#' { continue @@ -247,26 +248,30 @@ func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string][]string continue } - ipAddr := net.ParseIP(fields[0]) - if ipAddr == nil { + ip := net.ParseIP(fields[0]) + if ip == nil { continue } + for i := 1; i != len(fields); i++ { host := fields[i] if len(host) == 0 { break } + sharp := strings.IndexByte(host, '#') if sharp == 0 { - break // skip the rest of the line after # + // Skip the comments. + break } else if sharp > 0 { host = host[:sharp] } - a.updateTable(table, host, ipAddr) - a.updateTableRev(tableRev, host, ipAddr) + a.updateTable(table, host, ip) + a.updateTableRev(tableRev, host, ip) if sharp >= 0 { - break // skip the rest of the line after # + // Skip the comments again. + break } } } diff --git a/internal/util/helpers.go b/internal/util/helpers.go index 1b759f45..e7575ee1 100644 --- a/internal/util/helpers.go +++ b/internal/util/helpers.go @@ -94,7 +94,8 @@ func IsOpenWRT() bool { continue } - body, err := ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name())) + var body []byte + body, err = ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name())) if err != nil { continue } diff --git a/internal/util/network.go b/internal/util/network.go index 52507617..ad2f253b 100644 --- a/internal/util/network.go +++ b/internal/util/network.go @@ -69,7 +69,8 @@ func GetValidNetInterfacesForWeb() ([]*NetInterface, error) { var netInterfaces []*NetInterface for _, iface := range ifaces { - addrs, err := iface.Addrs() + var addrs []net.Addr + addrs, err = iface.Addrs() if err != nil { return nil, fmt.Errorf("failed to get addresses for interface %s: %w", iface.Name, err) } diff --git a/internal/util/tls.go b/internal/util/tls.go index 534231a6..f1aa4976 100644 --- a/internal/util/tls.go +++ b/internal/util/tls.go @@ -32,19 +32,24 @@ func LoadSystemRootCAs() *x509.CertPool { if !os.IsNotExist(err) { log.Error("Opening directory: %s: %s", dir, err) } + continue } + rootsAdded := false for _, fi := range fis { - data, err := ioutil.ReadFile(dir + "/" + fi.Name()) - if err == nil && roots.AppendCertsFromPEM(data) { + var certData []byte + certData, err = ioutil.ReadFile(dir + "/" + fi.Name()) + if err == nil && roots.AppendCertsFromPEM(certData) { rootsAdded = true } } + if rootsAdded { return roots } } + return nil }