Skip to content

Commit 0588281

Browse files
committed
fix panic when scale values are negative
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 9fb79a6 commit 0588281

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

loader/validate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ func checkConsistency(project *types.Project) error { //nolint:gocyclo
131131
s.Deploy.Replicas = s.Scale
132132
}
133133

134+
if s.Scale != nil && *s.Scale < 0 {
135+
return fmt.Errorf("services.%s.scale: must be greater than or equal to 0", s.Name)
136+
}
137+
if s.Deploy != nil && s.Deploy.Replicas != nil && *s.Deploy.Replicas < 0 {
138+
return fmt.Errorf("services.%s.deploy.replicas: must be greater than or equal to 0", s.Name)
139+
}
140+
134141
if s.CPUS != 0 && s.Deploy != nil {
135142
if s.Deploy.Resources.Limits != nil && s.Deploy.Resources.Limits.NanoCPUs.Value() != s.CPUS {
136143
return fmt.Errorf("services.%s: can't set distinct values on 'cpus' and 'deploy.resources.limits.cpus': %w",

loader/validate_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,31 @@ func TestValidateMountConflict(t *testing.T) {
436436
err := checkConsistency(project)
437437
assert.Error(t, err, "services.myservice.volumes[1]: target /conflict already mounted as services.myservice.tmpfs[1]")
438438
}
439+
440+
func TestValidateNegativeScale(t *testing.T) {
441+
project := &types.Project{
442+
Services: types.Services{
443+
"myservice": {
444+
Name: "myservice",
445+
Image: "scratch",
446+
Scale: ptr(-1),
447+
},
448+
},
449+
}
450+
err := checkConsistency(project)
451+
assert.Error(t, err, "services.myservice.scale: must be greater than or equal to 0")
452+
453+
project = &types.Project{
454+
Services: types.Services{
455+
"myservice": {
456+
Name: "myservice",
457+
Image: "scratch",
458+
Deploy: &types.DeployConfig{
459+
Replicas: ptr(-1),
460+
},
461+
},
462+
},
463+
}
464+
err = checkConsistency(project)
465+
assert.Error(t, err, "services.myservice.deploy.replicas: must be greater than or equal to 0")
466+
}

0 commit comments

Comments
 (0)