mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-12 15:50:15 -08:00
fix(conda): memory leak by adding closure method for package.json file (#9349)
Co-authored-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
This commit is contained in:
@@ -90,17 +90,7 @@ func findLicenseFromEnvDir(pkg types.Package, prefix string) ([]string, error) {
|
||||
return nil, xerrors.Errorf("incorrect packageJSON file pattern: %w", err)
|
||||
}
|
||||
if matched {
|
||||
file, err := os.Open(filepath.Join(condaMetaDir, entry.Name()))
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("unable to open packageJSON file: %w", err)
|
||||
}
|
||||
packageJson, _, err := meta.NewParser().Parse(file)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("unable to parse packageJSON file: %w", err)
|
||||
}
|
||||
// packageJson always contain only 1 element
|
||||
// cf. https://github.com/aquasecurity/trivy/blob/c3192f061d7e84eaf38df8df7c879dc00b4ca137/pkg/dependency/parser/conda/meta/parse.go#L39-L45
|
||||
return packageJson[0].Licenses, nil
|
||||
return licenseFromPackageJson(condaMetaDir, entry.Name())
|
||||
}
|
||||
}
|
||||
return nil, xerrors.Errorf("meta file didn't find")
|
||||
@@ -117,3 +107,20 @@ func (a environmentAnalyzer) Type() analyzer.Type {
|
||||
func (a environmentAnalyzer) Version() int {
|
||||
return version
|
||||
}
|
||||
|
||||
func licenseFromPackageJson(condaMetaDir, fileName string) ([]string, error) {
|
||||
file, err := os.Open(filepath.Join(condaMetaDir, fileName))
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("unable to open packageJSON file: %w", err)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
packageJson, _, err := meta.NewParser().Parse(file)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("unable to parse packageJSON file: %w", err)
|
||||
}
|
||||
// packageJson always contain only 1 element
|
||||
// cf. https://github.com/aquasecurity/trivy/blob/c3192f061d7e84eaf38df8df7c879dc00b4ca137/pkg/dependency/parser/conda/meta/parse.go#L39-L45
|
||||
return packageJson[0].Licenses, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user