Merge pull request #1697 from shirou/feature/fix_golangclilint_gosec

fix golangcilint errors, ignore gosec G115
pull/1653/merge
shirou 7 months ago committed by GitHub
commit 2c51005a94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -3,19 +3,19 @@ issues:
exclude-rules: exclude-rules:
- linters: - linters:
- gosec - gosec
text: "G204" text: G115
- linters: - linters:
- revive - revive
text: "var-naming" text: var-naming
- linters: - linters:
- revive - revive
text: "exported" text: exported
- linters: - linters:
- revive - revive
text: "empty-block" text: empty-block
- linters: - linters:
- revive - revive
text: "unused-parameter" text: unused-parameter
linters: linters:
enable: enable:
- asciicheck - asciicheck
@ -30,7 +30,6 @@ linters:
- gosec - gosec
- gosimple - gosimple
- importas - importas
- megacheck
- misspell - misspell
- nakedret - nakedret
- nolintlint - nolintlint
@ -39,14 +38,11 @@ linters:
- typecheck - typecheck
- unparam - unparam
disable: disable:
- deadcode
- errcheck - errcheck
- govet - govet
- ineffassign - ineffassign
- staticcheck - staticcheck
- structcheck
- unused - unused
- varcheck
linters-settings: linters-settings:
gci: gci:
sections: sections:

@ -11,9 +11,10 @@ import (
"strings" "strings"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v4/internal/common"
"github.com/tklauser/go-sysconf" "github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
var ( var (
@ -136,7 +137,7 @@ func parseDmesgBoot(fileName string) (InfoStat, int, error) {
c.Model = matches[4] c.Model = matches[4]
t, err := strconv.ParseInt(matches[5], 10, 32) t, err := strconv.ParseInt(matches[5], 10, 32)
if err != nil { 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) c.Stepping = int32(t)
} else if matches := featuresMatch.FindStringSubmatch(line); matches != nil { } else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {
@ -150,12 +151,12 @@ func parseDmesgBoot(fileName string) (InfoStat, int, error) {
} else if matches := cpuCores.FindStringSubmatch(line); matches != nil { } else if matches := cpuCores.FindStringSubmatch(line); matches != nil {
t, err := strconv.ParseInt(matches[1], 10, 32) t, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil { 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) cpuNum = int(t)
t2, err := strconv.ParseInt(matches[2], 10, 32) t2, err := strconv.ParseInt(matches[2], 10, 32)
if err != nil { 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) c.Cores = int32(t2)
} }

@ -395,7 +395,7 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
for _, line := range lines { for _, line := range lines {
line = strings.ToLower(line) line = strings.ToLower(line)
if strings.HasPrefix(line, "processor") { 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 { if err == nil {
ret++ ret++
} }
@ -464,11 +464,11 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
} }
fields[0] = strings.TrimSpace(fields[0]) fields[0] = strings.TrimSpace(fields[0])
if fields[0] == "physical id" || fields[0] == "cpu cores" { 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 { if err != nil {
continue continue
} }
currentInfo[fields[0]] = val currentInfo[fields[0]] = int(val)
} }
} }
ret := 0 ret := 0

@ -9,9 +9,10 @@ import (
"runtime" "runtime"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v4/internal/common"
"github.com/tklauser/go-sysconf" "github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
const ( const (

@ -136,7 +136,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return nil, err return nil, err
} }
case `%Used`: 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 { if err != nil {
return nil, err return nil, err
} }
@ -152,7 +152,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return nil, err return nil, err
} }
case `%Iused`: 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 { if err != nil {
return nil, err return nil, err
} }

@ -7,8 +7,9 @@ import (
"context" "context"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
const ( const (

@ -13,9 +13,10 @@ import (
"strings" "strings"
"unsafe" "unsafe"
"golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common" "github.com/shirou/gopsutil/v4/internal/common"
"github.com/shirou/gopsutil/v4/process" "github.com/shirou/gopsutil/v4/process"
"golang.org/x/sys/unix"
) )
const ( const (

@ -7,8 +7,9 @@ import (
"context" "context"
"strings" "strings"
"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
func HostIDWithContext(ctx context.Context) (string, error) { func HostIDWithContext(ctx context.Context) (string, error) {

@ -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) err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
if err == nil { if err == nil {
buildNumberStr := windows.UTF16ToString(regBuf[:]) 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) platform = strings.Replace(platform, "Windows 10", "Windows 11", 1)
} }
} }

@ -154,7 +154,7 @@ func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
var ret []string var ret []string
r := bufio.NewReader(f) 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') line, err := r.ReadString('\n')
if err != nil { if err != nil {
if err == io.EOF && len(line) > 0 { if err == io.EOF && len(line) > 0 {
@ -162,7 +162,7 @@ func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
} }
break break
} }
if i < int(offset) { if i < offset {
continue continue
} }
ret = append(ret, strings.Trim(line, "\n")) ret = append(ret, strings.Trim(line, "\n"))

@ -8,8 +8,9 @@ import (
"errors" "errors"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
@ -85,7 +86,6 @@ func SwapMemory() (*SwapMemoryStat, error) {
} }
// Constants from vm/vm_param.h // Constants from vm/vm_param.h
// nolint: golint
const ( const (
XSWDEV_VERSION11 = 1 XSWDEV_VERSION11 = 1
XSWDEV_VERSION = 2 XSWDEV_VERSION = 2

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

@ -143,8 +143,8 @@ func newMapInterfaceNameUsage(ifaces []netstatInterface) mapInterfaceNameUsage {
return output return output
} }
func (min mapInterfaceNameUsage) isTruncated() bool { func (mapi mapInterfaceNameUsage) isTruncated() bool {
for _, usage := range min { for _, usage := range mapi {
if usage > 1 { if usage > 1 {
return true return true
} }
@ -152,9 +152,9 @@ func (min mapInterfaceNameUsage) isTruncated() bool {
return false return false
} }
func (min mapInterfaceNameUsage) notTruncated() []string { func (mapi mapInterfaceNameUsage) notTruncated() []string {
output := make([]string, 0) output := make([]string, 0)
for ifaceName, usage := range min { for ifaceName, usage := range mapi {
if usage == 1 { if usage == 1 {
output = append(output, ifaceName) output = append(output, ifaceName)
} }
@ -247,7 +247,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
} }
} }
if pernic == false { if !pernic {
return getIOCountersAll(ret) return getIOCountersAll(ret)
} }
return ret, nil return ret, nil

@ -83,7 +83,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
ret = append(ret, n) ret = append(ret, n)
} }
if pernic == false { if !pernic {
return getIOCountersAll(ret) return getIOCountersAll(ret)
} }
@ -96,7 +96,7 @@ func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
} }
func IOCountersByFileWithContext(ctx context.Context, 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) { func FilterCounters() ([]FilterStat, error) {

@ -573,7 +573,7 @@ func getProcInodes(root string, pid int32, maxConn int) (map[string][]inodeMap,
if !ok { if !ok {
ret[inode] = make([]inodeMap, 0) ret[inode] = make([]inodeMap, 0)
} }
fd, err := strconv.Atoi(dirEntry.Name()) fd, err := strconv.ParseInt(dirEntry.Name(), 10, 32)
if err != nil { if err != nil {
continue continue
} }
@ -858,7 +858,7 @@ func processUnix(file string, kind netConnectionKindType, inodes map[string][]in
if len(tokens) < 6 { if len(tokens) < 6 {
continue continue
} }
st, err := strconv.Atoi(tokens[4]) st, err := strconv.ParseInt(tokens[4], 10, 32)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -52,27 +52,27 @@ func TestIOCountersByFileParsing(t *testing.T) {
assert.NotEmpty(t, counters) assert.NotEmpty(t, counters)
assert.Equal(t, 2, len(counters)) assert.Equal(t, 2, len(counters))
assert.Equal(t, interface0, counters[0].Name) assert.Equal(t, interface0, counters[0].Name)
assert.Equal(t, 1, int(counters[0].BytesRecv)) assert.Equal(t, uint64(1), counters[0].BytesRecv)
assert.Equal(t, 2, int(counters[0].PacketsRecv)) assert.Equal(t, uint64(2), counters[0].PacketsRecv)
assert.Equal(t, 3, int(counters[0].Errin)) assert.Equal(t, uint64(3), counters[0].Errin)
assert.Equal(t, 4, int(counters[0].Dropin)) assert.Equal(t, uint64(4), counters[0].Dropin)
assert.Equal(t, 5, int(counters[0].Fifoin)) assert.Equal(t, uint64(5), counters[0].Fifoin)
assert.Equal(t, 9, int(counters[0].BytesSent)) assert.Equal(t, uint64(9), counters[0].BytesSent)
assert.Equal(t, 10, int(counters[0].PacketsSent)) assert.Equal(t, uint64(10), counters[0].PacketsSent)
assert.Equal(t, 11, int(counters[0].Errout)) assert.Equal(t, uint64(11), counters[0].Errout)
assert.Equal(t, 12, int(counters[0].Dropout)) assert.Equal(t, uint64(12), counters[0].Dropout)
assert.Equal(t, 13, int(counters[0].Fifoout)) assert.Equal(t, uint64(13), counters[0].Fifoout)
assert.Equal(t, interface1, counters[1].Name) assert.Equal(t, interface1, counters[1].Name)
assert.Equal(t, 100, int(counters[1].BytesRecv)) assert.Equal(t, uint64(100), counters[1].BytesRecv)
assert.Equal(t, 200, int(counters[1].PacketsRecv)) assert.Equal(t, uint64(200), counters[1].PacketsRecv)
assert.Equal(t, 300, int(counters[1].Errin)) assert.Equal(t, uint64(300), counters[1].Errin)
assert.Equal(t, 400, int(counters[1].Dropin)) assert.Equal(t, uint64(400), counters[1].Dropin)
assert.Equal(t, 500, int(counters[1].Fifoin)) assert.Equal(t, uint64(500), counters[1].Fifoin)
assert.Equal(t, 900, int(counters[1].BytesSent)) assert.Equal(t, uint64(900), counters[1].BytesSent)
assert.Equal(t, 1000, int(counters[1].PacketsSent)) assert.Equal(t, uint64(1000), counters[1].PacketsSent)
assert.Equal(t, 1100, int(counters[1].Errout)) assert.Equal(t, uint64(1100), counters[1].Errout)
assert.Equal(t, 1200, int(counters[1].Dropout)) assert.Equal(t, uint64(1200), counters[1].Dropout)
assert.Equal(t, 1300, int(counters[1].Fifoout)) assert.Equal(t, uint64(1300), counters[1].Fifoout)
} }
err = tmpfile.Close() err = tmpfile.Close()
@ -81,7 +81,7 @@ func TestIOCountersByFileParsing(t *testing.T) {
func TestGetProcInodesAll(t *testing.T) { func TestGetProcInodesAll(t *testing.T) {
waitForServer := make(chan bool) 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 addr, err := net.ResolveTCPAddr("tcp", "localhost:0") // dynamically get a random open port from OS
if err != nil { if err != nil {
t.Skipf("unable to resolve localhost: %v", err) t.Skipf("unable to resolve localhost: %v", err)
@ -99,7 +99,7 @@ func TestGetProcInodesAll(t *testing.T) {
} }
defer conn.Close() defer conn.Close()
} }
}() }(t)
<-waitForServer <-waitForServer
root := common.HostProcWithContext(context.Background(), "") root := common.HostProcWithContext(context.Background(), "")

@ -97,7 +97,7 @@ func ParseNetstat(output string, mode string,
n.PacketsSent = parsed[2] n.PacketsSent = parsed[2]
n.Dropout = parsed[3] n.Dropout = parsed[3]
case "ine": case "ine":
n.Errin = parsed[0] n.Errin = parsed[0]
n.Errout = parsed[1] n.Errout = parsed[1]
} }
@ -255,7 +255,7 @@ func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, r
return Addr{}, fmt.Errorf("unknown family, %d", family) return Addr{}, fmt.Errorf("unknown family, %d", family)
} }
} }
lport, err := strconv.Atoi(port) lport, err := strconv.ParseInt(port, 10, 32)
if err != nil { if err != nil {
return Addr{}, err return Addr{}, err
} }

@ -109,11 +109,11 @@ func parseNetLine(line string) (ConnectionStat, error) {
f[7] = "unix" f[7] = "unix"
} }
pid, err := strconv.Atoi(f[1]) pid, err := strconv.ParseInt(f[1], 10, 32)
if err != nil { if err != nil {
return ConnectionStat{}, err return ConnectionStat{}, err
} }
fd, err := strconv.Atoi(strings.Trim(f[3], "u")) fd, err := strconv.ParseInt(strings.Trim(f[3], "u"), 10, 32)
if err != nil { if err != nil {
return ConnectionStat{}, fmt.Errorf("unknown fd, %s", f[3]) return ConnectionStat{}, fmt.Errorf("unknown fd, %s", f[3])
} }
@ -157,7 +157,7 @@ func parseNetAddr(line string) (laddr Addr, raddr Addr, err error) {
if err != nil { if err != nil {
return Addr{}, fmt.Errorf("wrong addr, %s", l) return Addr{}, fmt.Errorf("wrong addr, %s", l)
} }
lport, err := strconv.Atoi(port) lport, err := strconv.ParseInt(port, 10, 32)
if err != nil { if err != nil {
return Addr{}, err return Addr{}, err
} }

@ -193,24 +193,24 @@ func convertCPUTimes(s string) (ret float64, err error) {
_t := strings.Split(s, ":") _t := strings.Split(s, ":")
switch len(_t) { switch len(_t) {
case 3: case 3:
hour, err := strconv.Atoi(_t[0]) hour, err := strconv.ParseInt(_t[0], 10, 32)
if err != nil { if err != nil {
return ret, err return ret, err
} }
t += hour * 60 * 60 * clockTicks t += int(hour) * 60 * 60 * clockTicks
mins, err := strconv.Atoi(_t[1]) mins, err := strconv.ParseInt(_t[1], 10, 32)
if err != nil { if err != nil {
return ret, err return ret, err
} }
t += mins * 60 * clockTicks t += int(mins) * 60 * clockTicks
_tmp = _t[2] _tmp = _t[2]
case 2: case 2:
mins, err := strconv.Atoi(_t[0]) mins, err := strconv.ParseInt(_t[0], 10, 32)
if err != nil { if err != nil {
return ret, err return ret, err
} }
t += mins * 60 * clockTicks t += int(mins) * 60 * clockTicks
_tmp = _t[1] _tmp = _t[1]
case 1, 0: case 1, 0:
_tmp = s _tmp = s
@ -225,10 +225,10 @@ func convertCPUTimes(s string) (ret float64, err error) {
if err != nil { if err != nil {
return ret, err return ret, err
} }
h, err := strconv.Atoi(_t[0]) h, err := strconv.ParseInt(_t[0], 10, 32)
t += h * clockTicks t += int(h) * clockTicks
h, err = strconv.Atoi(_t[1]) h, err = strconv.ParseInt(_t[1], 10, 32)
t += h t += int(h)
return float64(t) / float64(clockTicks), nil return float64(t) / float64(clockTicks), nil
} }

@ -20,7 +20,7 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
func (p *Process) ExeWithContext(ctx context.Context) (string, error) { func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
out, err := invoke.CommandWithContext(ctx, "lsof", "-p", strconv.Itoa(int(p.Pid)), "-Fpfn") out, err := invoke.CommandWithContext(ctx, "lsof", "-p", strconv.Itoa(int(p.Pid)), "-Fpfn")
if err != nil { if err != nil {
return "", fmt.Errorf("bad call to lsof: %s", err) return "", fmt.Errorf("bad call to lsof: %w", err)
} }
txtFound := 0 txtFound := 0
lines := strings.Split(string(out), "\n") lines := strings.Split(string(out), "\n")
@ -111,15 +111,15 @@ func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, e
if err != nil { if err != nil {
return nil, err return nil, err
} }
rss, err := strconv.Atoi(r[0][0]) rss, err := strconv.ParseInt(r[0][0], 10, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
vms, err := strconv.Atoi(r[0][1]) vms, err := strconv.ParseInt(r[0][1], 10, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
pagein, err := strconv.Atoi(r[0][2]) pagein, err := strconv.ParseInt(r[0][2], 10, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -307,6 +307,7 @@ func TestName(t *testing.T) {
} }
} }
// #nosec G204
func TestLong_Name_With_Spaces(t *testing.T) { func TestLong_Name_With_Spaces(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "") tmpdir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
@ -353,6 +354,7 @@ func TestLong_Name_With_Spaces(t *testing.T) {
cmd.Process.Kill() cmd.Process.Kill()
} }
// #nosec G204
func TestLong_Name(t *testing.T) { func TestLong_Name(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "") tmpdir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
@ -779,6 +781,7 @@ func TestIsRunning(t *testing.T) {
} }
} }
// #nosec G204
func TestEnviron(t *testing.T) { func TestEnviron(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "") tmpdir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {

@ -1,7 +1,7 @@
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
//go:build openbsd //go:build openbsd
package openbsd package sensors
import ( import (
"context" "context"

Loading…
Cancel
Save