Skip to content

Commit 04b3064

Browse files
authored
chore: fix nil ptr deref on terraform plan data (#171)
Could hit a deref if the plan was empty.
1 parent a737d47 commit 04b3064

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

plan.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ func planJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t
8080

8181
// priorPlanModule returns the state data of the module a given block is in.
8282
func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateModule {
83+
if plan.PriorState == nil || plan.PriorState.Values == nil {
84+
return nil // No root module available in the plan, nothing to do
85+
}
86+
87+
rootModule := plan.PriorState.Values.RootModule
88+
8389
if !block.InModule() {
84-
return plan.PriorState.Values.RootModule
90+
// If the block is not in a module, then we can just return the root module.
91+
return rootModule
8592
}
8693

8794
var modPath []string
@@ -94,9 +101,12 @@ func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateMod
94101
}
95102
}
96103

97-
current := plan.PriorState.Values.RootModule
104+
current := rootModule
98105
for i := range modPath {
99106
idx := slices.IndexFunc(current.ChildModules, func(m *tfjson.StateModule) bool {
107+
if m == nil {
108+
return false
109+
}
100110
return m.Address == strings.Join(modPath[:i+1], ".")
101111
})
102112
if idx == -1 {

0 commit comments

Comments
 (0)