Skip to content

Commit 19b061c

Browse files
author
zhouhao
committed
generate: fix nil deference
Signed-off-by: zhouhao <[email protected]>
1 parent 6d2dbbc commit 19b061c

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

generate/generate.go

+8-17
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func (g *Generator) SetProcessArgs(args []string) {
398398

399399
// ClearProcessEnv clears g.spec.Process.Env.
400400
func (g *Generator) ClearProcessEnv() {
401-
if g.spec == nil {
401+
if g.spec == nil || g.spec.Process == nil {
402402
return
403403
}
404404
g.spec.Process.Env = []string{}
@@ -440,7 +440,7 @@ func (g *Generator) AddProcessRlimits(rType string, rHard uint64, rSoft uint64)
440440

441441
// RemoveProcessRlimits removes a rlimit from g.spec.Process.Rlimits.
442442
func (g *Generator) RemoveProcessRlimits(rType string) error {
443-
if g.spec == nil {
443+
if g.spec == nil || g.spec.Process == nil {
444444
return nil
445445
}
446446
for i, rlimit := range g.spec.Process.Rlimits {
@@ -454,15 +454,15 @@ func (g *Generator) RemoveProcessRlimits(rType string) error {
454454

455455
// ClearProcessRlimits clear g.spec.Process.Rlimits.
456456
func (g *Generator) ClearProcessRlimits() {
457-
if g.spec == nil {
457+
if g.spec == nil || g.spec.Process == nil {
458458
return
459459
}
460460
g.spec.Process.Rlimits = []rspec.POSIXRlimit{}
461461
}
462462

463463
// ClearProcessAdditionalGids clear g.spec.Process.AdditionalGids.
464464
func (g *Generator) ClearProcessAdditionalGids() {
465-
if g.spec == nil {
465+
if g.spec == nil || g.spec.Process == nil {
466466
return
467467
}
468468
g.spec.Process.User.AdditionalGids = []uint32{}
@@ -737,10 +737,7 @@ func (g *Generator) SetLinuxRootPropagation(rp string) error {
737737

738738
// ClearPreStartHooks clear g.spec.Hooks.Prestart.
739739
func (g *Generator) ClearPreStartHooks() {
740-
if g.spec == nil {
741-
return
742-
}
743-
if g.spec.Hooks == nil {
740+
if g.spec == nil || g.spec.Hooks == nil {
744741
return
745742
}
746743
g.spec.Hooks.Prestart = []rspec.Hook{}
@@ -787,10 +784,7 @@ func (g *Generator) AddPreStartHookTimeout(path string, timeout int) {
787784

788785
// ClearPostStopHooks clear g.spec.Hooks.Poststop.
789786
func (g *Generator) ClearPostStopHooks() {
790-
if g.spec == nil {
791-
return
792-
}
793-
if g.spec.Hooks == nil {
787+
if g.spec == nil || g.spec.Hooks == nil {
794788
return
795789
}
796790
g.spec.Hooks.Poststop = []rspec.Hook{}
@@ -837,10 +831,7 @@ func (g *Generator) AddPostStopHookTimeout(path string, timeout int) {
837831

838832
// ClearPostStartHooks clear g.spec.Hooks.Poststart.
839833
func (g *Generator) ClearPostStartHooks() {
840-
if g.spec == nil {
841-
return
842-
}
843-
if g.spec.Hooks == nil {
834+
if g.spec == nil || g.spec.Hooks == nil {
844835
return
845836
}
846837
g.spec.Hooks.Poststart = []rspec.Hook{}
@@ -976,7 +967,7 @@ func (g *Generator) SetupPrivileged(privileged bool) {
976967

977968
// ClearProcessCapabilities clear g.spec.Process.Capabilities.
978969
func (g *Generator) ClearProcessCapabilities() {
979-
if g.spec == nil {
970+
if g.spec == nil || g.spec.Process == nil || g.spec.Process.Capabilities == nil {
980971
return
981972
}
982973
g.spec.Process.Capabilities.Bounding = []string{}

0 commit comments

Comments
 (0)