gopsutil icon indicating copy to clipboard operation
gopsutil copied to clipboard

common.ReadInts() discrepancy

Open disconnect3d opened this issue 5 years ago • 1 comments

TL;DR: The docstring for the ReadInts func says it returns []int32 but it returns []int64. However, it parses integers as 32-bit integers.

Via https://github.com/shirou/gopsutil/blob/master/internal/common/common.go#L200-L218:


// ReadInts reads contents from single line file and returns them as []int32.
func ReadInts(filename string) ([]int64, error) {
	f, err := os.Open(filename)
	if err != nil {
		return []int64{}, err
	}
	defer f.Close()

	var ret []int64

	r := bufio.NewReader(f)

	// The int files that this is concerned with should only be one liners.
	line, err := r.ReadString('\n')
	if err != nil {
		return []int64{}, err
	}

	i, err := strconv.ParseInt(strings.Trim(line, "\n"), 10, 32)
	if err != nil {
		return []int64{}, err
	}
	ret = append(ret, i)

	return ret, nil
}

I'd expect either the function to return []int32 or the docstring to be fixed and the numbers to be parsed with ParseInt(..., 64).

disconnect3d avatar Aug 20 '20 09:08 disconnect3d

common.ReadInts() is only used to read /proc/sys/net/netfilter/nf_conntrack_count and /proc/sys/net/netfilter/nf_conntrack_max (only on linux).

Documentation only specifies INTEGER, without any bit size.

Looking through the source, I find two unsigned integer nf_conntrack_max instances https://github.com/torvalds/linux/blob/00e4db51259a5f936fec1424b884f029479d3981/net/netfilter/nf_conntrack_core.c#L181 and https://github.com/torvalds/linux/blob/00e4db51259a5f936fec1424b884f029479d3981/include/net/netfilter/nf_conntrack.h#L302, and I couldn't find a nf_conntrack_count variable (but a header file with this function returning an unsigned integer).

From that I guess maybe common.ReadInts should be renamed common.ReadUint64s (to handle both 32 and 64 archs) and should be using strconv.ParseUint (with a 64 bit size).

Lomanic avatar Aug 30 '20 18:08 Lomanic