diff --git a/cgroup1/utils.go b/cgroup1/utils.go index ae951687..2b7d5520 100644 --- a/cgroup1/utils.go +++ b/cgroup1/utils.go @@ -18,6 +18,7 @@ package cgroup1 import ( "bufio" + "bytes" "fmt" "os" "path/filepath" @@ -137,13 +138,19 @@ func readUint(path string) (uint64, error) { } defer f.Close() - b := make([]byte, 32) // Chose 32 as some files have leading/trailing whitespaces and alignment + // We should only need 20 bytes for the max uint64, but for a nice power of 2 + // lets use 32. + b := make([]byte, 32) n, err := f.Read(b) if err != nil { return 0, err } - - return parseUint(strings.TrimSpace(string(b[:n])), 10, 64) + s := string(bytes.TrimSpace(b[:n])) + if s == "max" { + // Return 0 for the max value to maintain backward compatibility. + return 0, nil + } + return parseUint(s, 10, 64) } func parseUint(s string, base, bitSize int) (uint64, error) { diff --git a/cgroup1/utils_test.go b/cgroup1/utils_test.go index 4fe5f367..847b860c 100644 --- a/cgroup1/utils_test.go +++ b/cgroup1/utils_test.go @@ -17,6 +17,8 @@ package cgroup1 import ( + "os" + "path/filepath" "testing" ) @@ -30,3 +32,20 @@ func BenchmarkReaduint64(b *testing.B) { } } } + +func TestReadUint(t *testing.T) { + tDir := t.TempDir() + pidsmax := filepath.Join(tDir, "pids.max") + err := os.WriteFile(pidsmax, []byte("max"), 0644) + if err != nil { + t.Fatal(err) + } + max, err := readUint(pidsmax) + if err != nil { + t.Fatal(err) + } + // test for backwards compatibility + if max != 0 { + t.Fail() + } +}