Skip to content

Commit

Permalink
fix golangcilint errors, ignore gosec G115
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Aug 23, 2024
1 parent 904a5a4 commit 37f5310
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 79 deletions.
14 changes: 5 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ issues:
exclude-rules:
- linters:
- gosec
text: "G204"
text: G115
- linters:
- revive
text: "var-naming"
text: var-naming
- linters:
- revive
text: "exported"
text: exported
- linters:
- revive
text: "empty-block"
text: empty-block
- linters:
- revive
text: "unused-parameter"
text: unused-parameter
linters:
enable:
- asciicheck
Expand All @@ -30,7 +30,6 @@ linters:
- gosec
- gosimple
- importas
- megacheck
- misspell
- nakedret
- nolintlint
Expand All @@ -39,14 +38,11 @@ linters:
- typecheck
- unparam
disable:
- deadcode
- errcheck
- govet
- ineffassign
- staticcheck
- structcheck
- unused
- varcheck
linters-settings:
gci:
sections:
Expand Down
9 changes: 5 additions & 4 deletions cpu/cpu_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"strings"
"unsafe"

"github.com/shirou/gopsutil/v4/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v4/internal/common"
)

var (
Expand Down Expand Up @@ -136,7 +137,7 @@ func parseDmesgBoot(fileName string) (InfoStat, int, error) {
c.Model = matches[4]
t, err := strconv.ParseInt(matches[5], 10, 32)
if err != nil {
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %v", line, err)
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %w", line, err)
}
c.Stepping = int32(t)
} else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {
Expand All @@ -150,12 +151,12 @@ func parseDmesgBoot(fileName string) (InfoStat, int, error) {
} else if matches := cpuCores.FindStringSubmatch(line); matches != nil {
t, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil {
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %v", line, err)
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %w", line, err)
}
cpuNum = int(t)
t2, err := strconv.ParseInt(matches[2], 10, 32)
if err != nil {
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %v", line, err)
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %w", line, err)
}
c.Cores = int32(t2)
}
Expand Down
6 changes: 3 additions & 3 deletions cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
for _, line := range lines {
line = strings.ToLower(line)
if strings.HasPrefix(line, "processor") {
_, err = strconv.Atoi(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]))
_, err = strconv.ParseInt(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]), 10, 32)
if err == nil {
ret++
}
Expand Down Expand Up @@ -464,11 +464,11 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
}
fields[0] = strings.TrimSpace(fields[0])
if fields[0] == "physical id" || fields[0] == "cpu cores" {
val, err := strconv.Atoi(strings.TrimSpace(fields[1]))
val, err := strconv.ParseInt(strings.TrimSpace(fields[1]), 10, 32)
if err != nil {
continue
}
currentInfo[fields[0]] = val
currentInfo[fields[0]] = int(val)
}
}
ret := 0
Expand Down
3 changes: 2 additions & 1 deletion cpu/cpu_netbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"runtime"
"unsafe"

"github.com/shirou/gopsutil/v4/internal/common"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v4/internal/common"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions disk/disk_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return nil, err
}
case `%Used`:
val, err := strconv.Atoi(strings.Replace(fs[i], "%", "", -1))
val, err := strconv.ParseInt(strings.Replace(fs[i], "%", "", -1), 10, 32)
if err != nil {
return nil, err
}
Expand All @@ -152,7 +152,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return nil, err
}
case `%Iused`:
val, err := strconv.Atoi(strings.Replace(fs[i], "%", "", -1))
val, err := strconv.ParseInt(strings.Replace(fs[i], "%", "", -1), 10, 32)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion disk/disk_netbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"context"
"unsafe"

"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v4/internal/common"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion host/host_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
"strings"
"unsafe"

"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v4/internal/common"
"github.com/shirou/gopsutil/v4/process"
"golang.org/x/sys/unix"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion host/host_netbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"context"
"strings"

"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v4/internal/common"
)

func HostIDWithContext(ctx context.Context) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion host/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func platformInformation(ctx context.Context) (platform, family, version, displa
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
if err == nil {
buildNumberStr := windows.UTF16ToString(regBuf[:])
if buildNumber, err := strconv.Atoi(buildNumberStr); err == nil && buildNumber >= 22000 {
if buildNumber, err := strconv.ParseInt(buildNumberStr, 10, 32); err == nil && buildNumber >= 22000 {
platform = strings.Replace(platform, "Windows 10", "Windows 11", 1)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
var ret []string

r := bufio.NewReader(f)
for i := 0; i < n+int(offset) || n < 0; i++ {
for i := uint(0); i < uint(n)+offset || n < 0; i++ {
line, err := r.ReadString('\n')
if err != nil {
if err == io.EOF && len(line) > 0 {
ret = append(ret, strings.Trim(line, "\n"))
}
break
}
if i < int(offset) {
if i < offset {
continue
}
ret = append(ret, strings.Trim(line, "\n"))
Expand Down
4 changes: 2 additions & 2 deletions mem/mem_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"errors"
"unsafe"

"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v4/internal/common"
)

func VirtualMemory() (*VirtualMemoryStat, error) {
Expand Down Expand Up @@ -85,7 +86,6 @@ func SwapMemory() (*SwapMemoryStat, error) {
}

// Constants from vm/vm_param.h
// nolint: golint
const (
XSWDEV_VERSION11 = 1
XSWDEV_VERSION = 2
Expand Down
2 changes: 1 addition & 1 deletion net/net_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, r
return Addr{}, fmt.Errorf("unknown family, %d", family)
}
}
lport, err := strconv.Atoi(port)
lport, err := strconv.ParseInt(port, 10, 32)
if err != nil {
return Addr{}, err
}
Expand Down
10 changes: 5 additions & 5 deletions net/net_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@ func newMapInterfaceNameUsage(ifaces []netstatInterface) mapInterfaceNameUsage {
return output
}

func (min mapInterfaceNameUsage) isTruncated() bool {
for _, usage := range min {
func (mapi mapInterfaceNameUsage) isTruncated() bool {
for _, usage := range mapi {
if usage > 1 {
return true
}
}
return false
}

func (min mapInterfaceNameUsage) notTruncated() []string {
func (mapi mapInterfaceNameUsage) notTruncated() []string {
output := make([]string, 0)
for ifaceName, usage := range min {
for ifaceName, usage := range mapi {
if usage == 1 {
output = append(output, ifaceName)
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
}
}

if pernic == false {
if !pernic {
return getIOCountersAll(ret)
}
return ret, nil
Expand Down
4 changes: 2 additions & 2 deletions net/net_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
ret = append(ret, n)
}

if pernic == false {
if !pernic {
return getIOCountersAll(ret)
}

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

func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
return IOCounters(pernic)
return IOCountersWithContext(ctx, pernic)
}

func FilterCounters() ([]FilterStat, error) {
Expand Down
4 changes: 2 additions & 2 deletions net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ func getProcInodes(root string, pid int32, maxConn int) (map[string][]inodeMap,
if !ok {
ret[inode] = make([]inodeMap, 0)
}
fd, err := strconv.Atoi(dirEntry.Name())
fd, err := strconv.ParseInt(dirEntry.Name(), 10, 32)
if err != nil {
continue
}
Expand Down Expand Up @@ -858,7 +858,7 @@ func processUnix(file string, kind netConnectionKindType, inodes map[string][]in
if len(tokens) < 6 {
continue
}
st, err := strconv.Atoi(tokens[4])
st, err := strconv.ParseInt(tokens[4], 10, 32)
if err != nil {
return nil, err
}
Expand Down
44 changes: 22 additions & 22 deletions net/net_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,27 @@ func TestIOCountersByFileParsing(t *testing.T) {
assert.NotEmpty(t, counters)
assert.Equal(t, 2, len(counters))
assert.Equal(t, interface0, counters[0].Name)
assert.Equal(t, 1, int(counters[0].BytesRecv))
assert.Equal(t, 2, int(counters[0].PacketsRecv))
assert.Equal(t, 3, int(counters[0].Errin))
assert.Equal(t, 4, int(counters[0].Dropin))
assert.Equal(t, 5, int(counters[0].Fifoin))
assert.Equal(t, 9, int(counters[0].BytesSent))
assert.Equal(t, 10, int(counters[0].PacketsSent))
assert.Equal(t, 11, int(counters[0].Errout))
assert.Equal(t, 12, int(counters[0].Dropout))
assert.Equal(t, 13, int(counters[0].Fifoout))
assert.Equal(t, uint64(1), counters[0].BytesRecv)
assert.Equal(t, uint64(2), counters[0].PacketsRecv)
assert.Equal(t, uint64(3), counters[0].Errin)
assert.Equal(t, uint64(4), counters[0].Dropin)
assert.Equal(t, uint64(5), counters[0].Fifoin)
assert.Equal(t, uint64(9), counters[0].BytesSent)
assert.Equal(t, uint64(10), counters[0].PacketsSent)
assert.Equal(t, uint64(11), counters[0].Errout)
assert.Equal(t, uint64(12), counters[0].Dropout)
assert.Equal(t, uint64(13), counters[0].Fifoout)
assert.Equal(t, interface1, counters[1].Name)
assert.Equal(t, 100, int(counters[1].BytesRecv))
assert.Equal(t, 200, int(counters[1].PacketsRecv))
assert.Equal(t, 300, int(counters[1].Errin))
assert.Equal(t, 400, int(counters[1].Dropin))
assert.Equal(t, 500, int(counters[1].Fifoin))
assert.Equal(t, 900, int(counters[1].BytesSent))
assert.Equal(t, 1000, int(counters[1].PacketsSent))
assert.Equal(t, 1100, int(counters[1].Errout))
assert.Equal(t, 1200, int(counters[1].Dropout))
assert.Equal(t, 1300, int(counters[1].Fifoout))
assert.Equal(t, uint64(100), counters[1].BytesRecv)
assert.Equal(t, uint64(200), counters[1].PacketsRecv)
assert.Equal(t, uint64(300), counters[1].Errin)
assert.Equal(t, uint64(400), counters[1].Dropin)
assert.Equal(t, uint64(500), counters[1].Fifoin)
assert.Equal(t, uint64(900), counters[1].BytesSent)
assert.Equal(t, uint64(1000), counters[1].PacketsSent)
assert.Equal(t, uint64(1100), counters[1].Errout)
assert.Equal(t, uint64(1200), counters[1].Dropout)
assert.Equal(t, uint64(1300), counters[1].Fifoout)
}

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

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

root := common.HostProcWithContext(context.Background(), "")
Expand Down
4 changes: 2 additions & 2 deletions net/net_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func ParseNetstat(output string, mode string,
n.PacketsSent = parsed[2]
n.Dropout = parsed[3]
case "ine":
n.Errin = parsed[0]
n.Errin = parsed[0]
n.Errout = parsed[1]
}

Expand Down Expand Up @@ -255,7 +255,7 @@ func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, r
return Addr{}, fmt.Errorf("unknown family, %d", family)
}
}
lport, err := strconv.Atoi(port)
lport, err := strconv.ParseInt(port, 10, 32)
if err != nil {
return Addr{}, err
}
Expand Down
Loading

0 comments on commit 37f5310

Please sign in to comment.