From 3c96821499cdfa01c716fb89f4e4ee1b3db7f638 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Wed, 6 Aug 2025 11:23:47 -0500 Subject: [PATCH 1/3] chore: fix nil ptr deref on terraform plan data --- plan.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plan.go b/plan.go index bcf44fa..5a0e974 100644 --- a/plan.go +++ b/plan.go @@ -81,7 +81,11 @@ func planJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t // priorPlanModule returns the state data of the module a given block is in. func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateModule { if !block.InModule() { - return plan.PriorState.Values.RootModule + // If the block is not in a module, then we can just return the root module. + if plan.PriorState != nil && plan.PriorState.Values != nil { + return plan.PriorState.Values.RootModule + } + return nil } var modPath []string From 8d8dc04ffaea8ed02616337b2902229e594add57 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Wed, 6 Aug 2025 11:26:12 -0500 Subject: [PATCH 2/3] more safety --- plan.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plan.go b/plan.go index 5a0e974..a2f844e 100644 --- a/plan.go +++ b/plan.go @@ -80,12 +80,15 @@ func planJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t // priorPlanModule returns the state data of the module a given block is in. func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateModule { + if plan.PriorState == nil || plan.PriorState.Values == nil { + return nil // No root module available in the plan, nothing to do + } + + rootModule := plan.PriorState.Values.RootModule + if !block.InModule() { // If the block is not in a module, then we can just return the root module. - if plan.PriorState != nil && plan.PriorState.Values != nil { - return plan.PriorState.Values.RootModule - } - return nil + return rootModule } var modPath []string @@ -98,9 +101,12 @@ func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateMod } } - current := plan.PriorState.Values.RootModule + current := rootModule for i := range modPath { idx := slices.IndexFunc(current.ChildModules, func(m *tfjson.StateModule) bool { + if m == nil { + return false // Avoid nil pointer dereference + } return m.Address == strings.Join(modPath[:i+1], ".") }) if idx == -1 { From 4446161a90326e33254a60d158e792414ad5ca0b Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Wed, 6 Aug 2025 11:37:10 -0500 Subject: [PATCH 3/3] remove redudant comment --- plan.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plan.go b/plan.go index a2f844e..ef19c36 100644 --- a/plan.go +++ b/plan.go @@ -105,7 +105,7 @@ func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateMod for i := range modPath { idx := slices.IndexFunc(current.ChildModules, func(m *tfjson.StateModule) bool { if m == nil { - return false // Avoid nil pointer dereference + return false } return m.Address == strings.Join(modPath[:i+1], ".") })