diff --git a/libcontainer/cgroups/fs/cpuacct.go b/libcontainer/cgroups/fs/cpuacct.go index d3bd7e111c7..e465b0921cf 100644 --- a/libcontainer/cgroups/fs/cpuacct.go +++ b/libcontainer/cgroups/fs/cpuacct.go @@ -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} }