From fcb86137874cb523ac48f37f9f27d0ed53670faf Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Fri, 22 Mar 2024 04:33:43 +0530 Subject: [PATCH] bugfix: `s/TrimRight/TrimSuffix` for certain cases Use `TrimSuffix` instead of `TrimRight` in cases where an exact suffix string needs to be removed, not all occurrences of all characters in a specified cutset. Signed-off-by: Pranshu Srivastava --- buddyinfo.go | 4 ++-- proc.go | 2 +- proc_smaps.go | 2 +- sysfs/system_cpu.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/buddyinfo.go b/buddyinfo.go index 4a173636..5519b815 100644 --- a/buddyinfo.go +++ b/buddyinfo.go @@ -58,8 +58,8 @@ func parseBuddyInfo(r io.Reader) ([]BuddyInfo, error) { return nil, fmt.Errorf("%w: Invalid number of fields, found: %v", ErrFileParse, parts) } - node := strings.TrimRight(parts[1], ",") - zone := strings.TrimRight(parts[3], ",") + node := strings.TrimSuffix(parts[1], ",") + zone := strings.TrimSuffix(parts[3], ",") arraySize := len(parts[4:]) if bucketCount == -1 { diff --git a/proc.go b/proc.go index d1f71caa..eb24a7e9 100644 --- a/proc.go +++ b/proc.go @@ -137,7 +137,7 @@ func (p Proc) CmdLine() ([]string, error) { return []string{}, nil } - return strings.Split(string(bytes.TrimRight(data, string("\x00"))), string(byte(0))), nil + return strings.Split(string(bytes.TrimRight(data, "\x00")), "\x00"), nil } // Wchan returns the wchan (wait channel) of a process. diff --git a/proc_smaps.go b/proc_smaps.go index ad8785a4..09060e82 100644 --- a/proc_smaps.go +++ b/proc_smaps.go @@ -127,7 +127,7 @@ func (s *ProcSMapsRollup) parseLine(line string) error { } v := strings.TrimSpace(kv[1]) - v = strings.TrimRight(v, " kB") + v = strings.TrimSuffix(v, " kB") vKBytes, err := strconv.ParseUint(v, 10, 64) if err != nil { diff --git a/sysfs/system_cpu.go b/sysfs/system_cpu.go index fbecc190..0e96e525 100644 --- a/sysfs/system_cpu.go +++ b/sysfs/system_cpu.go @@ -321,7 +321,7 @@ func parseCPURange(data []byte) ([]uint16, error) { var cpusInt = []uint16{} - for _, cpu := range strings.Split(strings.TrimRight(string(data), "\n"), ",") { + for _, cpu := range strings.Split(strings.TrimSuffix(string(data), "\n"), ",") { if cpu == "" { continue }