Skip to content

Commit c305051

Browse files
Fix lint and security issues
gosec was failing after the last update introduced some new checks.
1 parent 7699fc9 commit c305051

File tree

11 files changed

+22
-23
lines changed

11 files changed

+22
-23
lines changed

client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ func (c *Client) mCleaner(m map[string]*HostClient) {
607607
c.mLock.Lock()
608608
for k, v := range m {
609609
v.connsLock.Lock()
610-
/* #nosec G601 */
611610
if v.connsCount == 0 && atomic.LoadInt32(&v.pendingClientRequests) == 0 {
612611
delete(m, k)
613612
}
@@ -1430,7 +1429,7 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
14301429
return false, ErrHostClientRedirectToDifferentScheme
14311430
}
14321431

1433-
atomic.StoreUint32(&c.lastUseTime, uint32(time.Now().Unix()-startTimeUnix))
1432+
atomic.StoreUint32(&c.lastUseTime, uint32(time.Now().Unix()-startTimeUnix)) // #nosec G115
14341433

14351434
// Free up resources occupied by response before sending the request,
14361435
// so the GC may reclaim these resources (e.g. response body).
@@ -1917,7 +1916,7 @@ func (c *HostClient) nextAddr() string {
19171916
}
19181917
addr := c.addrs[0]
19191918
if len(c.addrs) > 1 {
1920-
addr = c.addrs[c.addrIdx%uint32(len(c.addrs))]
1919+
addr = c.addrs[c.addrIdx%uint32(len(c.addrs))] // #nosec G115
19211920
c.addrIdx++
19221921
}
19231922
c.addrsLock.Unlock()

examples/letsencrypt/letsencryptserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func main() {
2828
}
2929

3030
// Let's Encrypt tls-alpn-01 only works on port 443.
31-
ln, err := net.Listen("tcp4", "0.0.0.0:443") /* #nosec G102 */
31+
ln, err := net.Listen("tcp4", "0.0.0.0:443") // #nosec G102
3232
if err != nil {
3333
panic(err)
3434
}

fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ func (h *fsHandler) compressAndOpenFSFile(filePath, fileEncoding string) (*fsFil
14061406
}
14071407

14081408
if compressedFilePath != filePath {
1409-
if err := os.MkdirAll(filepath.Dir(compressedFilePath), os.ModePerm); err != nil {
1409+
if err := os.MkdirAll(filepath.Dir(compressedFilePath), 0o750); err != nil {
14101410
return nil, err
14111411
}
14121412
}

fuzz_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,38 @@ func FuzzVisitHeaderParams(f *testing.F) {
4242
func FuzzResponseReadLimitBody(f *testing.F) {
4343
f.Add([]byte("HTTP/1.1 200 OK\r\nContent-Type: aa\r\nContent-Length: 10\r\n\r\n9876543210"), 1024)
4444

45-
f.Fuzz(func(t *testing.T, body []byte, max int) {
46-
if len(body) > 1024*1024 || max > 1024*1024 {
45+
f.Fuzz(func(t *testing.T, body []byte, maxBodySize int) {
46+
if len(body) > 1024*1024 || maxBodySize > 1024*1024 {
4747
return
4848
}
4949
// Only test with a max for the body, otherwise a very large Content-Length will just OOM.
50-
if max <= 0 {
50+
if maxBodySize <= 0 {
5151
return
5252
}
5353

5454
res := AcquireResponse()
5555
defer ReleaseResponse(res)
5656

57-
_ = res.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), max)
57+
_ = res.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), maxBodySize)
5858
})
5959
}
6060

6161
func FuzzRequestReadLimitBody(f *testing.F) {
6262
f.Add([]byte("POST /a HTTP/1.1\r\nHost: a.com\r\nTransfer-Encoding: chunked\r\nContent-Type: aa\r\n\r\n6\r\nfoobar\r\n3\r\nbaz\r\n0\r\nfoobar\r\n\r\n"), 1024)
6363

64-
f.Fuzz(func(t *testing.T, body []byte, max int) {
65-
if len(body) > 1024*1024 || max > 1024*1024 {
64+
f.Fuzz(func(t *testing.T, body []byte, maxBodySize int) {
65+
if len(body) > 1024*1024 || maxBodySize > 1024*1024 {
6666
return
6767
}
6868
// Only test with a max for the body, otherwise a very large Content-Length will just OOM.
69-
if max <= 0 {
69+
if maxBodySize <= 0 {
7070
return
7171
}
7272

7373
req := AcquireRequest()
7474
defer ReleaseRequest(req)
7575

76-
_ = req.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), max)
76+
_ = req.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), maxBodySize)
7777
})
7878
}
7979

headers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const (
136136

137137
// WebSockets.
138138
HeaderSecWebSocketAccept = "Sec-WebSocket-Accept"
139-
HeaderSecWebSocketExtensions = "Sec-WebSocket-Extensions" /* #nosec G101 */
139+
HeaderSecWebSocketExtensions = "Sec-WebSocket-Extensions" // #nosec G101
140140
HeaderSecWebSocketKey = "Sec-WebSocket-Key"
141141
HeaderSecWebSocketProtocol = "Sec-WebSocket-Protocol"
142142
HeaderSecWebSocketVersion = "Sec-WebSocket-Version"

lbclient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (cc *LBClient) get() *lbClient {
139139
minT := atomic.LoadUint64(&minC.total)
140140
for _, c := range cs[1:] {
141141
n := c.PendingRequests()
142-
t := atomic.LoadUint64(&c.total) /* #nosec G601 */
142+
t := atomic.LoadUint64(&c.total)
143143
if n < minN || (n == minN && t < minT) {
144144
minC = c
145145
minN = n

prefork/prefork.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (p *Prefork) setTCPListenerFiles(addr string) error {
135135
}
136136

137137
func (p *Prefork) doCommand() (*exec.Cmd, error) {
138-
/* #nosec G204 */
138+
// #nosec G204
139139
cmd := exec.Command(os.Args[0], os.Args[1:]...)
140140
cmd.Stdout = os.Stdout
141141
cmd.Stderr = os.Stderr

round2_64.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ func roundUpForSliceCap(n int) int {
1212
return n
1313
}
1414

15-
x := uint64(n - 1)
15+
x := uint64(n - 1) // #nosec G115
1616
x |= x >> 1
1717
x |= x >> 2
1818
x |= x >> 4
1919
x |= x >> 8
2020
x |= x >> 16
2121

22-
return int(x + 1)
22+
return int(x + 1) // #nosec G115
2323
}

server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,8 +2034,8 @@ func (s *Server) ServeConn(c net.Conn) error {
20342034
c = pic
20352035
}
20362036

2037-
n := atomic.AddUint32(&s.concurrency, 1)
2038-
if n > uint32(s.getConcurrency()) {
2037+
n := int(atomic.AddUint32(&s.concurrency, 1)) // #nosec G115
2038+
if n > s.getConcurrency() {
20392039
atomic.AddUint32(&s.concurrency, ^uint32(0))
20402040
s.writeFastError(c, StatusServiceUnavailable, "The connection cannot be served because Server.Concurrency limit exceeded")
20412041
c.Close()
@@ -2415,7 +2415,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {
24152415
}
24162416

24172417
connectionClose = connectionClose ||
2418-
(s.MaxRequestsPerConn > 0 && connRequestNum >= uint64(s.MaxRequestsPerConn)) ||
2418+
(s.MaxRequestsPerConn > 0 && connRequestNum >= uint64(s.MaxRequestsPerConn)) || // #nosec G115
24192419
ctx.Response.Header.ConnectionClose() ||
24202420
(s.CloseOnShutdown && atomic.LoadInt32(&s.stop) == 1)
24212421
if connectionClose {

tcpdialer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func (d *TCPDialer) dial(addr string, dualStack bool, timeout time.Duration) (ne
298298
return nil, err
299299
}
300300
var conn net.Conn
301-
n := uint32(len(addrs))
301+
n := uint32(len(addrs)) // #nosec G115
302302
for n > 0 {
303303
conn, err = d.tryDial(network, addrs[idx%n].String(), deadline, d.concurrencyCh)
304304
if err == nil {

workerpool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (wp *workerPool) getCh() *workerChan {
176176
}
177177

178178
currentWorkers := atomic.LoadInt32(&wp.workersCount)
179-
if currentWorkers < int32(wp.MaxWorkersCount) {
179+
if int(currentWorkers) < wp.MaxWorkersCount {
180180
if atomic.CompareAndSwapInt32(&wp.workersCount, currentWorkers, currentWorkers+1) {
181181
ch = wp.workerChanPool.Get().(*workerChan)
182182
go wp.workerFunc(ch)

0 commit comments

Comments
 (0)