Repository navigation
fix(engine): guard Files.Lines against empty file panic - #32280
SudhanshuMatrix wants to merge 1 commit into
Conversation
Avoid panicking with index out of range when calling Lines on an empty file. Checking the length of the slice instead of checking for nil ensures empty files do not cause panic, and using strings.TrimSuffix makes newline trimming safer. Signed-off-by: Sudhanshu <[email protected]>
There was a problem hiding this comment.
Pull request overview
Fixes a deterministic runtime panic in the pkg/engine template helper .Files.Lines when the referenced chart file exists but is empty ([]byte{}), ensuring template rendering succeeds across Helm CLI and SDK usage.
Changes:
- Update
files.Linesto treat zero-length file contents as empty input and avoid out-of-range indexing. - Replace manual trailing newline removal with
strings.TrimSuffix. - Add unit test coverage for empty-file and non-existent-file cases in
TestLines.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/engine/files.go | Adds a zero-length guard and safe newline trimming to prevent panics in .Files.Lines. |
| pkg/engine/files_test.go | Extends TestLines with empty and missing file cases to validate the fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
kunalworldwide
left a comment
There was a problem hiding this comment.
The guard is correct — an empty file should return an empty slice, not panic. Using len(f[path]) == 0 also correctly handles the nil map entry case, so the behavior is unchanged for missing files.
Switching from manual index slicing to strings.TrimSuffix(s, "\n") is safer and handles the empty-string case naturally. The added test cases for empty and non-existent files cover the regression paths.
LGTM.
What this PR does / why we need it:
This PR fixes a runtime panic in the
.Files.Linestemplate helper function when referencing an empty file inside a chart.An empty file inside a chart is loaded and stored as a non-nil, zero-length byte slice (
[]byte{}). The function previously checked onlyf[path] == nil, which evaluated tofalse, and then attempted to trim the newline character usings[len(s)-1]on the empty strings, resulting in a panic:runtime error: index out of range [-1].To resolve this, we:
len(f[path]) == 0.strings.TrimSuffix(s, "\n").closes #32279
Special notes for your reviewer:
TestLinesinpkg/engine/files_test.go.If applicable:
docs neededlabel should be applied if so)