Skip to content

Commit 4198127

Browse files
committed
Fixed several issues reported by gosec and code analysis tools
1 parent d34a067 commit 4198127

12 files changed

+109
-48
lines changed

api.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func StartAPIServer(config *Config,
7373
}
7474
}()
7575

76-
personalBlockList := []string{}
76+
var personalBlockList []string
7777

7878
scanner := bufio.NewScanner(f)
7979
for scanner.Scan() {
@@ -124,8 +124,14 @@ func StartAPIServer(config *Config,
124124
logger.Critical("error while reading personal block list")
125125
return
126126
}
127-
f.Truncate(0)
128-
f.Write([]byte(personalBlockList))
127+
err := f.Truncate(0)
128+
if err != nil {
129+
logger.Error(err)
130+
}
131+
_, err = f.Write([]byte(personalBlockList))
132+
if err != nil {
133+
logger.Error(err)
134+
}
129135
}
130136
})
131137

cache.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,6 @@ func (e CacheIsFull) Error() string {
4343
return "Cache is Full"
4444
}
4545

46-
// SerializerError type
47-
type SerializerError struct {
48-
}
49-
50-
// Error formats an error for the SerializerError type
51-
func (e SerializerError) Error() string {
52-
return "Serializer error"
53-
}
54-
5546
// Mesg represents a cache entry
5647
type Mesg struct {
5748
Msg *dns.Msg
@@ -162,7 +153,7 @@ func (c *MemoryCache) Remove(key string) {
162153
c.mu.Unlock()
163154
}
164155

165-
// Exists returns whether or not a key exists in the cache
156+
// Exists returns whether a key exists in the cache
166157
func (c *MemoryCache) Exists(key string) bool {
167158
key = strings.ToLower(key)
168159

@@ -179,7 +170,7 @@ func (c *MemoryCache) Length() int {
179170
return len(c.Backend)
180171
}
181172

182-
// Full returns whether or not the cache is full
173+
// Full returns whether the cache is full
183174
func (c *MemoryCache) Full() bool {
184175
if c.Maxcount == 0 {
185176
return false
@@ -239,7 +230,7 @@ func (c *MemoryBlockCache) Remove(key string) {
239230
func (c *MemoryBlockCache) Set(key string, value bool) error {
240231
c.mu.Lock()
241232
if strings.HasPrefix(key, "~") {
242-
// get rid of the ~ prefix
233+
// get rid of the ~ prefix
243234
runes := []rune(key)
244235
ex := string(runes[1:])
245236
re, err := regexp.Compile(ex)
@@ -258,7 +249,7 @@ func (c *MemoryBlockCache) Set(key string, value bool) error {
258249
return nil
259250
}
260251

261-
// Exists returns whether or not a key exists in the cache
252+
// Exists returns whether a key exists in the cache
262253
func (c *MemoryBlockCache) Exists(key string) bool {
263254
key = strings.ToLower(key)
264255

drbl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ func drblCheckHostname(hostname string) bool {
88
testhost := ""
99
verdict := false
1010
if strings.HasSuffix(hostname, ".") {
11-
testhost = string(hostname[:len(hostname)-1])
11+
testhost = hostname[:len(hostname)-1]
1212
logger.Debug("a root query:", hostname)
1313
} else {
14-
testhost = string(hostname)
14+
testhost = hostname
1515
logger.Debug("not a root query:", hostname)
1616
}
1717
block, weight := drblPeers.Check(testhost)

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf h1:eg0MeVzsP1G42dRafH3vf+al2vQIJU0YHX+1Tw87oco=
44
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
5+
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
56
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
67
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
78
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -24,6 +25,7 @@ github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7a
2425
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
2526
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
2627
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
28+
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
2729
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
2830
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
2931
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
@@ -32,6 +34,7 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
3234
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
3335
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
3436
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
37+
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
3538
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
3639
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
3740
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -54,6 +57,7 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO
5457
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
5558
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735 h1:7YvPJVmEeFHR1Tj9sZEYsmarJEQfMVYpd/Vyy/A8dqE=
5659
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
60+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
5761
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5862
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5963
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
@@ -79,13 +83,17 @@ golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7w
7983
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
8084
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8185
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
86+
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
8287
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
8388
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
89+
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18 h1:xFbv3LvlvQAmbNJFCBKRv1Ccvnh9FVsW0FX2kTWWowE=
8490
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
91+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
8592
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
8693
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8794
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
8895
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
96+
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
8997
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
9098
gopkg.in/gin-contrib/cors.v1 v1.0.0-20170318125340-cf4846e6a636 h1:08a5y5i4Uab3iiIv0K3tgMkXIjunoSfqPzbnkOzt3Bc=
9199
gopkg.in/gin-contrib/cors.v1 v1.0.0-20170318125340-cf4846e6a636/go.mod h1:WNNqtZpyoJM1oJrh6efN7u6jd0EZYwjXlubLsIQUZRU=

grimd_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ func BenchmarkResolver(b *testing.B) {
1515
b.ResetTimer()
1616

1717
for i := 0; i < b.N; i++ {
18-
c.Exchange(m, testNameserver)
18+
_, _, err := c.Exchange(m, testNameserver)
19+
if err != nil {
20+
logger.Error(err)
21+
}
1922
}
2023
}

handler.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ func (h *DNSHandler) do(config *Config, blockCache *MemoryBlockCache, questionCa
9292
break
9393
}
9494
func(Net string, w dns.ResponseWriter, req *dns.Msg) {
95-
defer w.Close()
95+
defer func(w dns.ResponseWriter) {
96+
err := w.Close()
97+
if err != nil {
98+
}
99+
}(w)
96100
q := req.Question[0]
97101
Q := Question{UnFqdn(q.Name), dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]}
98102

@@ -137,7 +141,7 @@ func (h *DNSHandler) do(config *Config, blockCache *MemoryBlockCache, questionCa
137141
} else {
138142
logger.Debugf("%s hit cache\n", Q.String())
139143

140-
// we need this copy against concurrent modification of Id
144+
// we need this copy against concurrent modification of ID
141145
msg := *mesg
142146
msg.Id = req.Id
143147
h.WriteReplyMsg(w, &msg)

logger.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func parseLogConfig(logConfigString string) (*logConfig, error) {
6666
if match != nil {
6767
l, err := parseLogLevel(match[2])
6868
if err != nil {
69-
return nil, fmt.Errorf("Error while parsing '%s': %s", match[0], err.Error())
69+
return nil, fmt.Errorf("error while parsing '%s': %s", match[0], err.Error())
7070
}
7171
result.files = append(result.files, fileConfig{match[1], l})
7272
continue
@@ -76,7 +76,7 @@ func parseLogConfig(logConfigString string) (*logConfig, error) {
7676
if match != nil {
7777
l, err := parseLogLevel(match[2])
7878
if err != nil {
79-
return nil, fmt.Errorf("Error while parsing '%s': %s", match[0], err.Error())
79+
return nil, fmt.Errorf("error while parsing '%s': %s", match[0], err.Error())
8080
}
8181
switch match[1] {
8282
case "syslog":
@@ -88,7 +88,7 @@ func parseLogConfig(logConfigString string) (*logConfig, error) {
8888
}
8989
}
9090

91-
return nil, fmt.Errorf("Error: uknown log config fragment: '%s'", part)
91+
return nil, fmt.Errorf("error: uknown log config fragment: '%s'", part)
9292

9393
}
9494
return &result, nil
@@ -152,7 +152,10 @@ func createFileLoggers(files []fileConfig, moduleName string) ([]logging.Backend
152152
b, file, err := createFileLogger(f, moduleName)
153153
if err != nil {
154154
for _, toClose := range openFiles {
155-
toClose.Close()
155+
err := toClose.Close()
156+
if err != nil {
157+
return nil, nil, err
158+
}
156159
}
157160
return nil, nil, err
158161
}
@@ -204,7 +207,10 @@ func loggerInit(cfg string) (loggingState, error) {
204207

205208
func (s loggingState) cleanUp() {
206209
for _, f := range s.files {
207-
f.Close()
210+
err := f.Close()
211+
if err != nil {
212+
logger.Error(err)
213+
}
208214
}
209215
}
210216

logger_test.go

+32-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"io/ioutil"
6+
"log"
67
"os"
78
"testing"
89

@@ -22,7 +23,7 @@ func TestLogConfigParsing(t *testing.T) {
2223
in: "file:grimd.log@0",
2324
out: &logConfig{
2425
files: []fileConfig{
25-
fileConfig{
26+
{
2627
name: "grimd.log",
2728
level: logging.WARNING,
2829
},
@@ -34,11 +35,11 @@ func TestLogConfigParsing(t *testing.T) {
3435
in: "file:grimd.log@0,file:something.log@1",
3536
out: &logConfig{
3637
files: []fileConfig{
37-
fileConfig{
38+
{
3839
name: "grimd.log",
3940
level: logging.WARNING,
4041
},
41-
fileConfig{
42+
{
4243
name: "something.log",
4344
level: logging.INFO,
4445
},
@@ -49,17 +50,17 @@ func TestLogConfigParsing(t *testing.T) {
4950
{
5051
in: "file:grimd.log@aa",
5152
out: nil,
52-
err: fmt.Errorf("Error while parsing 'file:grimd.log@aa': 'aa' is not an integer"),
53+
err: fmt.Errorf("error while parsing 'file:grimd.log@aa': 'aa' is not an integer"),
5354
},
5455
{
5556
in: "syslog@aa",
5657
out: nil,
57-
err: fmt.Errorf("Error while parsing 'syslog@aa': 'aa' is not an integer"),
58+
err: fmt.Errorf("error while parsing 'syslog@aa': 'aa' is not an integer"),
5859
},
5960
{
6061
in: "fail:grimd.log@1",
6162
out: nil,
62-
err: fmt.Errorf("Error: uknown log config fragment: 'fail:grimd.log@1'"),
63+
err: fmt.Errorf("error: uknown log config fragment: 'fail:grimd.log@1'"),
6364
},
6465
{
6566
in: "syslog@0",
@@ -75,7 +76,7 @@ func TestLogConfigParsing(t *testing.T) {
7576
in: "file:grimd.log@1,syslog@1",
7677
out: &logConfig{
7778
files: []fileConfig{
78-
fileConfig{
79+
{
7980
name: "grimd.log",
8081
level: logging.INFO,
8182
},
@@ -101,7 +102,7 @@ func TestLogConfigParsing(t *testing.T) {
101102
in: "file:grimd.log@2,syslog@1,stderr@0",
102103
out: &logConfig{
103104
files: []fileConfig{
104-
fileConfig{
105+
{
105106
name: "grimd.log",
106107
level: logging.DEBUG,
107108
},
@@ -136,7 +137,11 @@ func TestCreateLogFile(t *testing.T) {
136137
t.SkipNow()
137138
dir, err := ioutil.TempDir("", "test")
138139
assert.Nil(t, err)
139-
defer os.RemoveAll(dir)
140+
defer func(path string) {
141+
err := os.RemoveAll(path)
142+
if err != nil {
143+
}
144+
}(dir)
140145

141146
var testCases = []struct {
142147
in string
@@ -159,8 +164,14 @@ func TestCreateLogFile(t *testing.T) {
159164
assert.Equal(t, test.err, err)
160165
if err == nil {
161166
assert.NotNil(t, result)
162-
result.Close()
163-
os.Remove(test.in)
167+
err := result.Close()
168+
if err != nil {
169+
logger.Critical(err)
170+
}
171+
err = os.Remove(test.in)
172+
if err != nil {
173+
logger.Critical(err)
174+
}
164175
}
165176
})
166177
}
@@ -170,7 +181,12 @@ func TestCreateFileLogger(t *testing.T) {
170181
t.SkipNow()
171182
dir, err := ioutil.TempDir("", "test")
172183
assert.Nil(t, err)
173-
defer os.RemoveAll(dir)
184+
defer func(path string) {
185+
err := os.RemoveAll(path)
186+
if err != nil {
187+
logger.Critical(err)
188+
}
189+
}(dir)
174190

175191
var testCases = []struct {
176192
in string
@@ -189,7 +205,10 @@ func TestCreateFileLogger(t *testing.T) {
189205
if err == nil {
190206
assert.NotNil(t, logger)
191207
assert.NotNil(t, file)
192-
file.Close()
208+
err := file.Close()
209+
if err != nil {
210+
log.Println(err)
211+
}
193212
}
194213
})
195214
}

main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func reloadBlockCache(config *Config,
4646
}
4747

4848
if config.UseDrbl > 0 {
49-
drblPeers, _ = drblpeer.NewPeerListFromYamlFile(config.DrblPeersFilename, config.DrblBlockWeight, config.DrblTimeout, (config.DrblDebug > 0))
49+
drblPeers, _ = drblpeer.NewPeerListFromYamlFile(config.DrblPeersFilename, config.DrblBlockWeight, config.DrblTimeout, config.DrblDebug > 0)
5050
if config.DrblDebug > 0 {
5151
log.Println("Drbl Debug is ON")
5252
drblPeers.Debug = true
@@ -120,7 +120,10 @@ forever:
120120
break forever
121121
case syscall.SIGHUP:
122122
logger.Error("SIGHUP received: rotating logs\n")
123-
loggingState.reopen()
123+
err := loggingState.reopen()
124+
if err != nil {
125+
logger.Error(err)
126+
}
124127
}
125128
case <-reloadChan:
126129
blockCache, apiServer, err = reloadBlockCache(config, blockCache, questionCache, drblPeers, apiServer, server, reloadChan)

resolver.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"net/http"
89
"strings"
@@ -135,7 +136,11 @@ func (r *Resolver) DoHLookup(url string, timeout int, req *dns.Msg) (*dns.Msg, e
135136
return nil, ResolvError{qname, "HTTPS", []string{url}}
136137
}
137138

138-
defer resp.Body.Close()
139+
defer func(Body io.ReadCloser) {
140+
err := Body.Close()
141+
if err != nil {
142+
}
143+
}(resp.Body)
139144

140145
//Check the request went ok
141146
if resp.StatusCode != http.StatusOK {

0 commit comments

Comments
 (0)