Skip to content

Commit

Permalink
Fix data types for ignored stat fields #401
Browse files Browse the repository at this point in the history
Some ignored fields in the /proc/[pid]/stat file may have values that are bigger than the max value of the current Go type that is used for the ignored variable. That leads to issues in runtime on some systems that use the maximum possible values for the related fields.

See for details:
* https://man7.org/linux/man-pages/man5/proc.5.html
* https://man7.org/linux/man-pages/man3/scanf.3.html

Signed-off-by: Vyacheslav Kulakov <kulakov.home@gmail.com>
  • Loading branch information
vykulakov authored and discordianfish committed Jul 27, 2021
1 parent f8ee60a commit e979fa4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
33 changes: 17 additions & 16 deletions proc_stat.go
Expand Up @@ -128,7 +128,8 @@ func (p Proc) Stat() (ProcStat, error) {
}

var (
ignore int
ignoreInt64 int64
ignoreUint64 uint64

s = ProcStat{PID: p.PID, proc: p.fs}
l = bytes.Index(data, []byte("("))
Expand Down Expand Up @@ -160,25 +161,25 @@ func (p Proc) Stat() (ProcStat, error) {
&s.Priority,
&s.Nice,
&s.NumThreads,
&ignore,
&ignoreInt64,
&s.Starttime,
&s.VSize,
&s.RSS,
&s.RSSLimit,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignore,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreInt64,
&ignoreInt64,
&s.RTPriority,
&s.Policy,
&s.DelayAcctBlkIOTicks,
Expand Down
12 changes: 12 additions & 0 deletions proc_stat_test.go
Expand Up @@ -76,6 +76,18 @@ func TestProcStat(t *testing.T) {
}
}

func TestProcStatIgnored(t *testing.T) {
p, err := getProcFixtures(t).Proc(26232)
if err != nil {
t.Fatal(err)
}

_, err = p.Stat()
if err != nil {
t.Errorf("want not error, have %s", err)
}
}

func TestProcStatComm(t *testing.T) {
s1, err := testProcStat(26231)
if err != nil {
Expand Down

0 comments on commit e979fa4

Please sign in to comment.