Skip to content

Commit 074353a

Browse files
committed
Handle gracefully any panic that occurs when building the SSA representation of a package
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent ec31a3a commit 074353a

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

analyzer.go

+34-22
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
282282
return pkgs, nil
283283
}
284284

285-
// CheckRules runs analysis on the given package
285+
// CheckRules runs analysis on the given package.
286286
func (gosec *Analyzer) CheckRules(pkg *packages.Package) {
287287
gosec.logger.Println("Checking package:", pkg.Name)
288288
for _, file := range pkg.Syntax {
@@ -318,31 +318,14 @@ func (gosec *Analyzer) CheckRules(pkg *packages.Package) {
318318
}
319319
}
320320

321-
// CheckAnalyzers runs analyzers on a given package
321+
// CheckAnalyzers runs analyzers on a given package.
322322
func (gosec *Analyzer) CheckAnalyzers(pkg *packages.Package) {
323-
ssaPass := &analysis.Pass{
324-
Analyzer: buildssa.Analyzer,
325-
Fset: pkg.Fset,
326-
Files: pkg.Syntax,
327-
OtherFiles: pkg.OtherFiles,
328-
IgnoredFiles: pkg.IgnoredFiles,
329-
Pkg: pkg.Types,
330-
TypesInfo: pkg.TypesInfo,
331-
TypesSizes: pkg.TypesSizes,
332-
ResultOf: nil,
333-
Report: nil,
334-
ImportObjectFact: nil,
335-
ExportObjectFact: nil,
336-
ImportPackageFact: nil,
337-
ExportPackageFact: nil,
338-
AllObjectFacts: nil,
339-
AllPackageFacts: nil,
340-
}
341-
ssaResult, err := ssaPass.Analyzer.Run(ssaPass)
323+
ssaResult, err := gosec.buildSSA(pkg)
342324
if err != nil {
343-
gosec.logger.Printf("Error running SSA analyser on package %q: %s", pkg.Name, err)
325+
gosec.logger.Printf("Error building the SSA representation of the package %q: %s", pkg.Name, err)
344326
return
345327
}
328+
346329
resultMap := map[*analysis.Analyzer]interface{}{
347330
buildssa.Analyzer: &analyzers.SSAAnalyzerResult{
348331
Config: gosec.Config(),
@@ -384,6 +367,35 @@ func (gosec *Analyzer) CheckAnalyzers(pkg *packages.Package) {
384367
}
385368
}
386369

370+
// buildSSA runs the SSA pass which builds the SSA representation of the package. It handles gracefully any panic.
371+
func (gosec *Analyzer) buildSSA(pkg *packages.Package) (interface{}, error) {
372+
defer func() {
373+
if r := recover(); r != nil {
374+
gosec.logger.Printf("Panic when running SSA analyser on package %q: %s", pkg.Name, r)
375+
}
376+
}()
377+
ssaPass := &analysis.Pass{
378+
Analyzer: buildssa.Analyzer,
379+
Fset: pkg.Fset,
380+
Files: pkg.Syntax,
381+
OtherFiles: pkg.OtherFiles,
382+
IgnoredFiles: pkg.IgnoredFiles,
383+
Pkg: pkg.Types,
384+
TypesInfo: pkg.TypesInfo,
385+
TypesSizes: pkg.TypesSizes,
386+
ResultOf: nil,
387+
Report: nil,
388+
ImportObjectFact: nil,
389+
ExportObjectFact: nil,
390+
ImportPackageFact: nil,
391+
ExportPackageFact: nil,
392+
AllObjectFacts: nil,
393+
AllPackageFacts: nil,
394+
}
395+
396+
return ssaPass.Analyzer.Run(ssaPass)
397+
}
398+
387399
func isGeneratedFile(file *ast.File) bool {
388400
for _, comment := range file.Comments {
389401
for _, row := range comment.List {

0 commit comments

Comments
 (0)