Skip to content

Commit b3cefcd

Browse files
committed
fs: error if cpuacct.usage_all format differs
We can reasonably expect the first three fields of cpuacct.usage_all to be "cpu user system", and if we see something different, it doesn't make sense to continue. So do check that the header is as expected. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 455b4a5 commit b3cefcd

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

fs/cpuacct.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fs
22

33
import (
44
"bufio"
5+
"fmt"
56
"os"
67
"strconv"
78
"strings"
@@ -129,7 +130,15 @@ func getPercpuUsageInModes(path string) ([]uint64, []uint64, error) {
129130
defer fd.Close()
130131

131132
scanner := bufio.NewScanner(fd)
132-
scanner.Scan() // skipping header line
133+
scanner.Scan() // Read header line.
134+
const want = "cpu user system"
135+
if !strings.HasPrefix(scanner.Text(), want) {
136+
return nil, nil, &parseError{
137+
Path: path,
138+
File: file,
139+
Err: fmt.Errorf("bad header (want %q, got %q)", want, scanner.Text()),
140+
}
141+
}
133142

134143
for scanner.Scan() {
135144
// Each line is: cpu user system

0 commit comments

Comments
 (0)