Skip to content

Commit b3d77ec

Browse files
authored
fix: Avoid trimming characters incorrectly from ProcStatus (#469)
Signed-off-by: Irvin Lim <irvinlimweiquan@gmail.com> Signed-off-by: Irvin Lim <irvinlimweiquan@gmail.com>
1 parent bf36102 commit b3d77ec

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

proc_status.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ func (p Proc) NewStatus() (ProcStatus, error) {
9696
kv := strings.SplitN(line, ":", 2)
9797

9898
// removes spaces
99-
k := string(strings.TrimSpace(kv[0]))
100-
v := string(strings.TrimSpace(kv[1]))
99+
k := strings.TrimSpace(kv[0])
100+
v := strings.TrimSpace(kv[1])
101101
// removes "kB"
102-
v = string(bytes.Trim([]byte(v), " kB"))
102+
v = strings.TrimSuffix(v, " kB")
103103

104104
// value to int when possible
105105
// we can skip error check here, 'cause vKBytes is not used when value is a string

proc_status_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ func TestProcStatusName(t *testing.T) {
7676
}
7777
}
7878

79+
func TestProcStatusNameTrim(t *testing.T) {
80+
p, err := getProcFixtures(t).Proc(26235)
81+
if err != nil {
82+
t.Fatal(err)
83+
}
84+
s, err := p.NewStatus()
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
if want, have := "kube-proxy", s.Name; want != have {
89+
t.Errorf("want name %s, have %s", want, have)
90+
}
91+
}
92+
7993
func TestProcStatusUIDs(t *testing.T) {
8094
p, err := getProcFixtures(t).Proc(26231)
8195
if err != nil {

testdata/fixtures.ttar

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,64 @@ Lines: 4
748748
40000000-40015000 r-xp 00000000 03:01 61874 /lib/ld-2.3.2.so
749749
Mode: 644
750750
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
751+
Directory: fixtures/proc/26235
752+
Mode: 755
753+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
754+
Path: fixtures/proc/26235/status
755+
Lines: 51
756+
Name: kube-proxy
757+
Umask: 0022
758+
State: S (sleeping)
759+
Tgid: 26235
760+
Ngid: 12345
761+
Pid: 26235
762+
PPid: 1234
763+
TracerPid: 0
764+
Uid: 0 0 0 0
765+
Gid: 0 0 0 0
766+
FDSize: 64
767+
Groups:
768+
NStgid: 26235 1
769+
NSpid: 26235 1
770+
NSpgid: 26235 1
771+
NSsid: 26235 1
772+
VmPeak: 758200 kB
773+
VmSize: 758200 kB
774+
VmLck: 0 kB
775+
VmPin: 0 kB
776+
VmHWM: 61776 kB
777+
VmRSS: 42652 kB
778+
RssAnon: 24852 kB
779+
RssFile: 17800 kB
780+
RssShmem: 0 kB
781+
VmData: 117136 kB
782+
VmStk: 132 kB
783+
VmExe: 21568 kB
784+
VmLib: 4 kB
785+
VmPTE: 264 kB
786+
VmSwap: 0 kB
787+
HugetlbPages: 0 kB
788+
Threads: 51
789+
SigQ: 9/511324
790+
SigPnd: 0000000000000000
791+
ShdPnd: 0000000000000000
792+
SigBlk: 0000000000000000
793+
SigIgn: 0000000000000000
794+
SigCgt: fffffffc7fc1feff
795+
CapInh: 0000003fffffffff
796+
CapPrm: 0000003fffffffff
797+
CapEff: 0000003fffffffff
798+
CapBnd: 0000003fffffffff
799+
CapAmb: 0000000000000000
800+
Seccomp: 0
801+
Cpus_allowed: ff
802+
Cpus_allowed_list: 0-7
803+
Mems_allowed: 00000000,00000001
804+
Mems_allowed_list: 0
805+
voluntary_ctxt_switches: 4742839
806+
nonvoluntary_ctxt_switches: 1727500
807+
Mode: 644
808+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
751809
Directory: fixtures/proc/584
752810
Mode: 755
753811
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

0 commit comments

Comments
 (0)