Skip to content

Commit

Permalink
libcontainer/cgroups/fs: replace strings.Fields with strings.SplitN
Browse files Browse the repository at this point in the history
Signed-off-by: Stavros Panakakis <[email protected]>
  • Loading branch information
Stavrospanakakis committed Sep 15, 2024
1 parent 1590491 commit a1e7cca
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions libcontainer/cgroups/fs/cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,31 @@ func getCpuUsageBreakdown(path string) (uint64, uint64, error) {
if err != nil {
return 0, 0, err
}
// TODO: use strings.SplitN instead.
fields := strings.Fields(data)
if len(fields) < 4 || fields[0] != userField || fields[2] != systemField {

lines := strings.SplitN(data, "\n", 2)
if len(lines) < 2 {
return 0, 0, malformedLine(path, file, data)
}

userLine := lines[0]
userLineFields := strings.SplitN(userLine, " ", 2)
if len(userLineFields) != 2 || userLineFields[0] != userField {
return 0, 0, malformedLine(path, file, data)
}
if userModeUsage, err = strconv.ParseUint(fields[1], 10, 64); err != nil {

userUsageInTicks := strings.TrimSpace(userLineFields[1])
if userModeUsage, err = strconv.ParseUint(userUsageInTicks, 10, 64); err != nil {
return 0, 0, &parseError{Path: path, File: file, Err: err}
}
if kernelModeUsage, err = strconv.ParseUint(fields[3], 10, 64); err != nil {

systemLine := lines[1]
systemLineFields := strings.SplitN(systemLine, " ", 2)
if len(systemLineFields) != 2 || systemLineFields[0] != systemField {
return 0, 0, malformedLine(path, file, data)
}

systemUsageInTicks := strings.TrimSpace(systemLineFields[1])
if kernelModeUsage, err = strconv.ParseUint(systemUsageInTicks, 10, 64); err != nil {
return 0, 0, &parseError{Path: path, File: file, Err: err}
}

Expand Down

0 comments on commit a1e7cca

Please sign in to comment.