Skip to content

Commit

Permalink
Fix panic in NewSystemd on nil values
Browse files Browse the repository at this point in the history
NewSystemd previously panicked if it was given a nil resource value for
resources.Memory.Max or resources.CPU.Weight (if resources.CPU or
resources.Memory is non-nil)

This also checks the value of Max and Weight before dereferencing them.

Signed-off-by: Cameron Sparr <[email protected]>
  • Loading branch information
sparrc committed Feb 17, 2022
1 parent 1df7813 commit 2cb7322
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions v2/cpuv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ func TestSystemdCgroupCpuController(t *testing.T) {
checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10))
}

func TestSystemdCgroupCpuController_NilWeight(t *testing.T) {
checkCgroupMode(t)
group := "testingCpuNilWeight.slice"
// nil weight defaults to 100
var quota int64 = 10000
var period uint64 = 8000
cpuMax := NewCPUMax(&quota, &period)
res := Resources{
CPU: &CPU{
Weight: nil,
Max: cpuMax,
},
}
c, err := NewSystemd("/", group, -1, &res)
if err != nil {
t.Fatal("failed to init new cgroup systemd manager: ", err)
}
defer c.DeleteSystemd()
}

func TestExtractQuotaAndPeriod(t *testing.T) {
var (
period uint64
Expand Down
4 changes: 2 additions & 2 deletions v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,12 @@ func NewSystemd(slice, group string, pid int, resources *Resources) (*Manager, e
properties = append(properties, newSystemdProperty("PIDs", []uint32{uint32(pid)}))
}

if resources.Memory != nil && *resources.Memory.Max != 0 {
if resources.Memory != nil && resources.Memory.Max != nil && *resources.Memory.Max != 0 {
properties = append(properties,
newSystemdProperty("MemoryMax", uint64(*resources.Memory.Max)))
}

if resources.CPU != nil && *resources.CPU.Weight != 0 {
if resources.CPU != nil && resources.CPU.Weight != nil && *resources.CPU.Weight != 0 {
properties = append(properties,
newSystemdProperty("CPUWeight", *resources.CPU.Weight))
}
Expand Down

0 comments on commit 2cb7322

Please sign in to comment.