From 1adce6be192245a6a13651e398e17d6c7f9147f5 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sun, 12 May 2024 22:02:56 +0530 Subject: [PATCH] bugfix: `s/TrimRight/TrimSuffix` for certain cases (#618) 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. Fixes: #507 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 eb88d78a..83807500 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 0e8c4fa0..14279636 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 }