Skip to content

Commit

Permalink
Merge pull request containerd#219 from sparrc/fix-panic
Browse files Browse the repository at this point in the history
Fix panic in NewSystemd on nil values
  • Loading branch information
estesp authored Feb 17, 2022
2 parents 1df7813 + 65478b8 commit cf1b326
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions v2/cpuv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ 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,
},
}
_, err := NewSystemd("/", group, -1, &res)
if err != nil {
t.Fatal("failed to init new cgroup systemd manager: ", err)
}
}

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 cf1b326

Please sign in to comment.