Skip to content

Commit

Permalink
Merge pull request #15 from vincentgna/fix-stack-overflow
Browse files Browse the repository at this point in the history
fix: avoid OOM and infinite loops in moduleInfo.load()
  • Loading branch information
vincentgna authored Feb 20, 2024
2 parents ed6e271 + fd0e70a commit 12e28bb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-fork.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
release-type: simple
default-branch: release-fork
release-as: 0.27.0-pre.1
release-as: 0.28.0-pre.1
changelog-types: ${{ steps.configure-changelog.outputs.result }}
# https://github.com/google-github-actions/release-please-action#github-credentials
token: ${{ secrets.VINCENT_PAT }}
Expand Down
62 changes: 35 additions & 27 deletions server/events/mocks/mock_command_requirement_handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 38 additions & 26 deletions server/events/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,51 @@ func (t tfFs) ReadDir(dirname string) ([]os.FileInfo, error) {
var _ tfconfig.FS = tfFs{}

func (m moduleInfo) load(files fs.FS, dir string, projects ...string) (_ *module, diags tfconfig.Diagnostics) {
if _, set := m[dir]; !set {
tfFiles := tfFs{files}
var mod *tfconfig.Module
mod, diags = tfconfig.LoadModuleFromFilesystem(tfFiles, dir)

deps := make(map[string]bool)
if mod != nil {
for _, c := range mod.ModuleCalls {
mPath := path.Join(dir, c.Source)
if !tfconfig.IsModuleDirOnFilesystem(tfFiles, mPath) {
continue
dependenciesHandled := map[string]struct{}{dir: {}} // Only look at each dependency once
todo := []string{dir}

for len(todo) > 0 {
dir := todo[len(todo)-1] // order of processing list not important, pick last for efficiency
todo = todo[:len(todo)-1]

if _, set := m[dir]; !set {
tfFiles := tfFs{files}
var mod *tfconfig.Module
mod, diag := tfconfig.LoadModuleFromFilesystem(tfFiles, dir)
if diag != nil {
diags = append(diags, diag...)
}

deps := make(map[string]bool)
if mod != nil {
for _, c := range mod.ModuleCalls {
mPath := path.Join(dir, c.Source)
if !tfconfig.IsModuleDirOnFilesystem(tfFiles, mPath) {
continue
}
deps[mPath] = true
}
deps[mPath] = true
}

m[dir] = &module{
path: dir,
dependencies: deps,
projects: make(map[string]bool),
}
}

m[dir] = &module{
path: dir,
dependencies: deps,
projects: make(map[string]bool),
// set projects on my dependencies
for dep := range m[dir].dependencies {
if _, exists := dependenciesHandled[dep]; !exists {
dependenciesHandled[dep] = struct{}{}
todo = append(todo, dep)
}
}
}
// set projects on my dependencies
for dep := range m[dir].dependencies {
_, err := m.load(files, dep, projects...)
if err != nil {
diags = append(diags, err...)
// add projects to the list of dependant projects
for _, p := range projects {
m[dir].projects[p] = true
}
}
// add projects to the list of dependant projects
for _, p := range projects {
m[dir].projects[p] = true
}
return m[dir], diags
}

Expand Down

0 comments on commit 12e28bb

Please sign in to comment.