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 bd59f35
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions libcontainer/cgroups/fs/cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,29 @@ 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 {

if userModeUsage, err = strconv.ParseUint(userLineFields[1], 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)
}

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

Expand Down

0 comments on commit bd59f35

Please sign in to comment.