Skip to content

Commit 2c51005

Browse files
authored
Merge pull request #1697 from shirou/feature/fix_golangclilint_gosec
fix golangcilint errors, ignore gosec G115
2 parents 904a5a4 + 37f5310 commit 2c51005

22 files changed

+83
-79
lines changed

.golangci.yml

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ issues:
33
exclude-rules:
44
- linters:
55
- gosec
6-
text: "G204"
6+
text: G115
77
- linters:
88
- revive
9-
text: "var-naming"
9+
text: var-naming
1010
- linters:
1111
- revive
12-
text: "exported"
12+
text: exported
1313
- linters:
1414
- revive
15-
text: "empty-block"
15+
text: empty-block
1616
- linters:
1717
- revive
18-
text: "unused-parameter"
18+
text: unused-parameter
1919
linters:
2020
enable:
2121
- asciicheck
@@ -30,7 +30,6 @@ linters:
3030
- gosec
3131
- gosimple
3232
- importas
33-
- megacheck
3433
- misspell
3534
- nakedret
3635
- nolintlint
@@ -39,14 +38,11 @@ linters:
3938
- typecheck
4039
- unparam
4140
disable:
42-
- deadcode
4341
- errcheck
4442
- govet
4543
- ineffassign
4644
- staticcheck
47-
- structcheck
4845
- unused
49-
- varcheck
5046
linters-settings:
5147
gci:
5248
sections:

cpu/cpu_freebsd.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
"strings"
1212
"unsafe"
1313

14-
"github.com/shirou/gopsutil/v4/internal/common"
1514
"github.com/tklauser/go-sysconf"
1615
"golang.org/x/sys/unix"
16+
17+
"github.com/shirou/gopsutil/v4/internal/common"
1718
)
1819

1920
var (
@@ -136,7 +137,7 @@ func parseDmesgBoot(fileName string) (InfoStat, int, error) {
136137
c.Model = matches[4]
137138
t, err := strconv.ParseInt(matches[5], 10, 32)
138139
if err != nil {
139-
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %v", line, err)
140+
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %w", line, err)
140141
}
141142
c.Stepping = int32(t)
142143
} else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {
@@ -150,12 +151,12 @@ func parseDmesgBoot(fileName string) (InfoStat, int, error) {
150151
} else if matches := cpuCores.FindStringSubmatch(line); matches != nil {
151152
t, err := strconv.ParseInt(matches[1], 10, 32)
152153
if err != nil {
153-
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %v", line, err)
154+
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %w", line, err)
154155
}
155156
cpuNum = int(t)
156157
t2, err := strconv.ParseInt(matches[2], 10, 32)
157158
if err != nil {
158-
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %v", line, err)
159+
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %w", line, err)
159160
}
160161
c.Cores = int32(t2)
161162
}

cpu/cpu_linux.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
395395
for _, line := range lines {
396396
line = strings.ToLower(line)
397397
if strings.HasPrefix(line, "processor") {
398-
_, err = strconv.Atoi(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]))
398+
_, err = strconv.ParseInt(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]), 10, 32)
399399
if err == nil {
400400
ret++
401401
}
@@ -464,11 +464,11 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
464464
}
465465
fields[0] = strings.TrimSpace(fields[0])
466466
if fields[0] == "physical id" || fields[0] == "cpu cores" {
467-
val, err := strconv.Atoi(strings.TrimSpace(fields[1]))
467+
val, err := strconv.ParseInt(strings.TrimSpace(fields[1]), 10, 32)
468468
if err != nil {
469469
continue
470470
}
471-
currentInfo[fields[0]] = val
471+
currentInfo[fields[0]] = int(val)
472472
}
473473
}
474474
ret := 0

cpu/cpu_netbsd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"runtime"
1010
"unsafe"
1111

12-
"github.com/shirou/gopsutil/v4/internal/common"
1312
"github.com/tklauser/go-sysconf"
1413
"golang.org/x/sys/unix"
14+
15+
"github.com/shirou/gopsutil/v4/internal/common"
1516
)
1617

1718
const (

disk/disk_aix_nocgo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
136136
return nil, err
137137
}
138138
case `%Used`:
139-
val, err := strconv.Atoi(strings.Replace(fs[i], "%", "", -1))
139+
val, err := strconv.ParseInt(strings.Replace(fs[i], "%", "", -1), 10, 32)
140140
if err != nil {
141141
return nil, err
142142
}
@@ -152,7 +152,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
152152
return nil, err
153153
}
154154
case `%Iused`:
155-
val, err := strconv.Atoi(strings.Replace(fs[i], "%", "", -1))
155+
val, err := strconv.ParseInt(strings.Replace(fs[i], "%", "", -1), 10, 32)
156156
if err != nil {
157157
return nil, err
158158
}

disk/disk_netbsd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"context"
88
"unsafe"
99

10-
"github.com/shirou/gopsutil/v4/internal/common"
1110
"golang.org/x/sys/unix"
11+
12+
"github.com/shirou/gopsutil/v4/internal/common"
1213
)
1314

1415
const (

host/host_freebsd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
"strings"
1414
"unsafe"
1515

16+
"golang.org/x/sys/unix"
17+
1618
"github.com/shirou/gopsutil/v4/internal/common"
1719
"github.com/shirou/gopsutil/v4/process"
18-
"golang.org/x/sys/unix"
1920
)
2021

2122
const (

host/host_netbsd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"context"
88
"strings"
99

10-
"github.com/shirou/gopsutil/v4/internal/common"
1110
"golang.org/x/sys/unix"
11+
12+
"github.com/shirou/gopsutil/v4/internal/common"
1213
)
1314

1415
func HostIDWithContext(ctx context.Context) (string, error) {

host/host_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func platformInformation(ctx context.Context) (platform, family, version, displa
181181
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
182182
if err == nil {
183183
buildNumberStr := windows.UTF16ToString(regBuf[:])
184-
if buildNumber, err := strconv.Atoi(buildNumberStr); err == nil && buildNumber >= 22000 {
184+
if buildNumber, err := strconv.ParseInt(buildNumberStr, 10, 32); err == nil && buildNumber >= 22000 {
185185
platform = strings.Replace(platform, "Windows 10", "Windows 11", 1)
186186
}
187187
}

internal/common/common.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
154154
var ret []string
155155

156156
r := bufio.NewReader(f)
157-
for i := 0; i < n+int(offset) || n < 0; i++ {
157+
for i := uint(0); i < uint(n)+offset || n < 0; i++ {
158158
line, err := r.ReadString('\n')
159159
if err != nil {
160160
if err == io.EOF && len(line) > 0 {
161161
ret = append(ret, strings.Trim(line, "\n"))
162162
}
163163
break
164164
}
165-
if i < int(offset) {
165+
if i < offset {
166166
continue
167167
}
168168
ret = append(ret, strings.Trim(line, "\n"))

mem/mem_freebsd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"errors"
99
"unsafe"
1010

11-
"github.com/shirou/gopsutil/v4/internal/common"
1211
"golang.org/x/sys/unix"
12+
13+
"github.com/shirou/gopsutil/v4/internal/common"
1314
)
1415

1516
func VirtualMemory() (*VirtualMemoryStat, error) {
@@ -85,7 +86,6 @@ func SwapMemory() (*SwapMemoryStat, error) {
8586
}
8687

8788
// Constants from vm/vm_param.h
88-
// nolint: golint
8989
const (
9090
XSWDEV_VERSION11 = 1
9191
XSWDEV_VERSION = 2

net/net_aix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, r
117117
return Addr{}, fmt.Errorf("unknown family, %d", family)
118118
}
119119
}
120-
lport, err := strconv.Atoi(port)
120+
lport, err := strconv.ParseInt(port, 10, 32)
121121
if err != nil {
122122
return Addr{}, err
123123
}

net/net_darwin.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,18 @@ func newMapInterfaceNameUsage(ifaces []netstatInterface) mapInterfaceNameUsage {
143143
return output
144144
}
145145

146-
func (min mapInterfaceNameUsage) isTruncated() bool {
147-
for _, usage := range min {
146+
func (mapi mapInterfaceNameUsage) isTruncated() bool {
147+
for _, usage := range mapi {
148148
if usage > 1 {
149149
return true
150150
}
151151
}
152152
return false
153153
}
154154

155-
func (min mapInterfaceNameUsage) notTruncated() []string {
155+
func (mapi mapInterfaceNameUsage) notTruncated() []string {
156156
output := make([]string, 0)
157-
for ifaceName, usage := range min {
157+
for ifaceName, usage := range mapi {
158158
if usage == 1 {
159159
output = append(output, ifaceName)
160160
}
@@ -247,7 +247,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
247247
}
248248
}
249249

250-
if pernic == false {
250+
if !pernic {
251251
return getIOCountersAll(ret)
252252
}
253253
return ret, nil

net/net_freebsd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
8383
ret = append(ret, n)
8484
}
8585

86-
if pernic == false {
86+
if !pernic {
8787
return getIOCountersAll(ret)
8888
}
8989

@@ -96,7 +96,7 @@ func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
9696
}
9797

9898
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
99-
return IOCounters(pernic)
99+
return IOCountersWithContext(ctx, pernic)
100100
}
101101

102102
func FilterCounters() ([]FilterStat, error) {

net/net_linux.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ func getProcInodes(root string, pid int32, maxConn int) (map[string][]inodeMap,
573573
if !ok {
574574
ret[inode] = make([]inodeMap, 0)
575575
}
576-
fd, err := strconv.Atoi(dirEntry.Name())
576+
fd, err := strconv.ParseInt(dirEntry.Name(), 10, 32)
577577
if err != nil {
578578
continue
579579
}
@@ -858,7 +858,7 @@ func processUnix(file string, kind netConnectionKindType, inodes map[string][]in
858858
if len(tokens) < 6 {
859859
continue
860860
}
861-
st, err := strconv.Atoi(tokens[4])
861+
st, err := strconv.ParseInt(tokens[4], 10, 32)
862862
if err != nil {
863863
return nil, err
864864
}

net/net_linux_test.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,27 @@ func TestIOCountersByFileParsing(t *testing.T) {
5252
assert.NotEmpty(t, counters)
5353
assert.Equal(t, 2, len(counters))
5454
assert.Equal(t, interface0, counters[0].Name)
55-
assert.Equal(t, 1, int(counters[0].BytesRecv))
56-
assert.Equal(t, 2, int(counters[0].PacketsRecv))
57-
assert.Equal(t, 3, int(counters[0].Errin))
58-
assert.Equal(t, 4, int(counters[0].Dropin))
59-
assert.Equal(t, 5, int(counters[0].Fifoin))
60-
assert.Equal(t, 9, int(counters[0].BytesSent))
61-
assert.Equal(t, 10, int(counters[0].PacketsSent))
62-
assert.Equal(t, 11, int(counters[0].Errout))
63-
assert.Equal(t, 12, int(counters[0].Dropout))
64-
assert.Equal(t, 13, int(counters[0].Fifoout))
55+
assert.Equal(t, uint64(1), counters[0].BytesRecv)
56+
assert.Equal(t, uint64(2), counters[0].PacketsRecv)
57+
assert.Equal(t, uint64(3), counters[0].Errin)
58+
assert.Equal(t, uint64(4), counters[0].Dropin)
59+
assert.Equal(t, uint64(5), counters[0].Fifoin)
60+
assert.Equal(t, uint64(9), counters[0].BytesSent)
61+
assert.Equal(t, uint64(10), counters[0].PacketsSent)
62+
assert.Equal(t, uint64(11), counters[0].Errout)
63+
assert.Equal(t, uint64(12), counters[0].Dropout)
64+
assert.Equal(t, uint64(13), counters[0].Fifoout)
6565
assert.Equal(t, interface1, counters[1].Name)
66-
assert.Equal(t, 100, int(counters[1].BytesRecv))
67-
assert.Equal(t, 200, int(counters[1].PacketsRecv))
68-
assert.Equal(t, 300, int(counters[1].Errin))
69-
assert.Equal(t, 400, int(counters[1].Dropin))
70-
assert.Equal(t, 500, int(counters[1].Fifoin))
71-
assert.Equal(t, 900, int(counters[1].BytesSent))
72-
assert.Equal(t, 1000, int(counters[1].PacketsSent))
73-
assert.Equal(t, 1100, int(counters[1].Errout))
74-
assert.Equal(t, 1200, int(counters[1].Dropout))
75-
assert.Equal(t, 1300, int(counters[1].Fifoout))
66+
assert.Equal(t, uint64(100), counters[1].BytesRecv)
67+
assert.Equal(t, uint64(200), counters[1].PacketsRecv)
68+
assert.Equal(t, uint64(300), counters[1].Errin)
69+
assert.Equal(t, uint64(400), counters[1].Dropin)
70+
assert.Equal(t, uint64(500), counters[1].Fifoin)
71+
assert.Equal(t, uint64(900), counters[1].BytesSent)
72+
assert.Equal(t, uint64(1000), counters[1].PacketsSent)
73+
assert.Equal(t, uint64(1100), counters[1].Errout)
74+
assert.Equal(t, uint64(1200), counters[1].Dropout)
75+
assert.Equal(t, uint64(1300), counters[1].Fifoout)
7676
}
7777

7878
err = tmpfile.Close()
@@ -81,7 +81,7 @@ func TestIOCountersByFileParsing(t *testing.T) {
8181

8282
func TestGetProcInodesAll(t *testing.T) {
8383
waitForServer := make(chan bool)
84-
go func() { // TCP listening goroutine to have some opened inodes even in CI
84+
go func(t *testing.T) { // TCP listening goroutine to have some opened inodes even in CI
8585
addr, err := net.ResolveTCPAddr("tcp", "localhost:0") // dynamically get a random open port from OS
8686
if err != nil {
8787
t.Skipf("unable to resolve localhost: %v", err)
@@ -99,7 +99,7 @@ func TestGetProcInodesAll(t *testing.T) {
9999
}
100100
defer conn.Close()
101101
}
102-
}()
102+
}(t)
103103
<-waitForServer
104104

105105
root := common.HostProcWithContext(context.Background(), "")

net/net_openbsd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func ParseNetstat(output string, mode string,
9797
n.PacketsSent = parsed[2]
9898
n.Dropout = parsed[3]
9999
case "ine":
100-
n.Errin = parsed[0]
100+
n.Errin = parsed[0]
101101
n.Errout = parsed[1]
102102
}
103103

@@ -255,7 +255,7 @@ func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, r
255255
return Addr{}, fmt.Errorf("unknown family, %d", family)
256256
}
257257
}
258-
lport, err := strconv.Atoi(port)
258+
lport, err := strconv.ParseInt(port, 10, 32)
259259
if err != nil {
260260
return Addr{}, err
261261
}

0 commit comments

Comments
 (0)