An empty module in state can panic

An empty module, or a module with an empty path can panic during graph
traversal. Make sure we clear these out when reading and writing the
state.
This commit is contained in:
James Bardin 2016-11-18 17:59:07 -05:00
parent 3920460220
commit def55e52ca
2 changed files with 45 additions and 0 deletions

View File

@ -644,6 +644,17 @@ func (s *State) init() {
}
s.ensureHasLineage()
// Filter out empty modules.
// A module is always assumed to have a path, and it's length isn't always
// bounds checked later on. Modules may be "emptied" during destroy, but we
// never want to store those in the state.
for i := 0; i < len(s.Modules); i++ {
if s.Modules[i] == nil || len(s.Modules[i].Path) == 0 {
s.Modules = append(s.Modules[:i], s.Modules[i+1:]...)
i--
}
}
for _, mod := range s.Modules {
mod.init()
}

View File

@ -1674,3 +1674,37 @@ func TestParseResourceStateKey(t *testing.T) {
}
}
}
func TestStateModuleOrphans_empty(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
},
&ModuleState{
Path: []string{RootModuleName, "foo", "bar"},
},
&ModuleState{
Path: []string{},
},
nil,
},
}
state.init()
// just calling this to check for panic
state.ModuleOrphans(RootModulePath, nil)
for _, mod := range state.Modules {
if mod == nil {
t.Fatal("found nil module")
}
if mod.Path == nil {
t.Fatal("found nil module path")
}
if len(mod.Path) == 0 {
t.Fatal("found empty module path")
}
}
}