fix: prevent nil pointer panic in profiling, unchecked JSON decode, and index out of bounds in lint#32397
Open
magic-peach wants to merge 1 commit into
Open
fix: prevent nil pointer panic in profiling, unchecked JSON decode, and index out of bounds in lint#32397magic-peach wants to merge 1 commit into
magic-peach wants to merge 1 commit into
Conversation
…nd 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes three bugs that can cause crashes or silent data loss:
1. Nil pointer panic in
stopProfiling(pkg/cmd/profiling.go)When
os.Create(memProfilePath)fails,fis nil. The code registereddefer f.Close()unconditionally, and also calledpprof.WriteHeapProfile(f)on the nil writer. This causes a nil pointer dereference panic when the deferredClose()runs.Fix: Move the
defer f.Close()andWriteHeapProfilecall inside anelseblock that only executes whenos.Createsucceeds.2. Unchecked JSON decode in
SearchWithContext(internal/monocular/search.go)The error from
json.NewDecoder(res.Body).Decode(result)was completely discarded. If the server returns malformed JSON (e.g., an HTML error page), the function silently returns(nil, nil), making it impossible for callers to distinguish "no results" from a parse error.Fix: Check and return the decode error with a descriptive message.
3. Index out of bounds panic in
lintChart(pkg/action/lint.go)After extracting a
.tgztotempDir, the code accessesfiles[0]without checking if the slice is empty. A malformed tarball that produces an empty directory causes an index out of range runtime error.Fix: Add a bounds check for empty directory before accessing
files[0].Related Issues
Testing
Each fix handles its edge case correctly:
Checklist