fix: prevent nil pointer panic in profiling, unchecked JSON decode, and index out of bounds in lint

- Fix stopProfiling to only register defer f.Close() when os.Create
  succeeds, preventing nil pointer dereference when file creation fails
- Check error from json.NewDecoder().Decode() in SearchWithContext
  instead of silently discarding it
- Add bounds check for empty directory after tarball extraction in
  lintChart to prevent index out of range panic

Signed-off-by: Akanksha Trehun <akankshatrehun@gmail.com>
pull/32397/head
Akanksha Trehun 2 weeks ago
parent ccb8f595ab
commit cc8b430d46
No known key found for this signature in database
GPG Key ID: 41CCD6D196B43BB5

@ -142,7 +142,9 @@ func (c *Client) SearchWithContext(ctx context.Context, term string) ([]SearchRe
result := &searchResponse{}
json.NewDecoder(res.Body).Decode(result)
if err := json.NewDecoder(res.Body).Decode(result); err != nil {
return nil, fmt.Errorf("failed to decode search results from %s: %w", p.String(), err)
}
return result.Data, nil
}

@ -112,6 +112,9 @@ func lintChart(path string, vals map[string]any, namespace string, kubeVersion *
if err != nil {
return linter, fmt.Errorf("unable to read temporary output directory %s: %w", tempDir, err)
}
if len(files) == 0 {
return linter, fmt.Errorf("temporary output directory %s is empty after extraction", tempDir)
}
if !files[0].IsDir() {
return linter, fmt.Errorf("unexpected file %s in temporary output directory %s", files[0].Name(), tempDir)
}

@ -74,12 +74,13 @@ func stopProfiling() error {
f, err := os.Create(memProfilePath)
if err != nil {
errs = append(errs, err)
}
defer f.Close()
} else {
defer f.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
errs = append(errs, err)
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
errs = append(errs, err)
}
}
}

Loading…
Cancel
Save