Skip to content

fix(engine): prevent Files.Lines panic on empty file (backport to v3) - #32303

Merged
TerryHowe merged 1 commit into
helm:dev-v3from
mahesh-sadupalli:fix/files-lines-empty-panic-v3
Jul 28, 2026
Merged

fix(engine): prevent Files.Lines panic on empty file (backport to v3)#32303
TerryHowe merged 1 commit into
helm:dev-v3from
mahesh-sadupalli:fix/files-lines-empty-panic-v3

Conversation

@mahesh-sadupalli

Copy link
Copy Markdown
Contributor

Backport of #32290 to dev-v3.

Problem

Files.Lines guards against a nil entry (f[path] == nil) but an empty file inside a chart archive is stored as a non-nil zero-length []byte. The guard passes, the code then indexes s[len(s)-1] to strip a trailing newline, and panics with an index-out-of-range. The engine recovers the panic into a render error, so every helm install, helm upgrade, or helm lint that references an empty file with {{ .Files.Lines ... }} fails.

Fix

Change the guard from f[path] == nil to len(f[path]) == 0, returning an empty slice for both missing and empty files (matching the documented behaviour for missing files).

Tests

Three new table-driven cases:

Case Expected
Empty file ("") empty slice (no panic)
Newline-only ("\n") [""] — one empty element
Missing file empty slice (pre-existing behaviour, regression guard)

Tests verified red on unfixed code (panic recovered as "index out of range"), green on fixed code.

Relation to main

This is a direct cherry-pick of the same commit from #32290 (targeting main). Cherry-pick applied cleanly with no conflicts. Requested as a v3 backport because the v3 bug-fix window closes today (8 July 2026).

Files.Lines guards against a nil entry but an empty file inside a
chart is stored as a non-nil zero-length byte slice, so the trailing
newline check indexes s[-1] and panics. The engine recovers the panic
into a render error, making every template/install/upgrade/lint that
references the file fail.

Extend the guard to len(f[path]) == 0 and return an empty slice,
matching the behaviour for missing files.

Fixes helm#32279

Signed-off-by: Mahesh Sadupalli <mahesh.sadupalli@gmail.com>
@pull-request-size pull-request-size Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 8, 2026
@promptless-for-oss

Copy link
Copy Markdown

Promptless prepared a documentation update related to this change.

Triggered by helm/helm PR #32303

This PR fixes a user-visible panic in the .Files.Lines chart template helper for empty files. The Chart Template Guide's "Accessing Files Inside Templates" → Lines section didn't document empty- or missing-file behavior, so the docs now note that Lines returns an empty list (not an error) for empty or missing files. Applied to both the v3 docs (this PR's target) and current/v4 docs, since this is the v3 backport of #32290.

Review: Clarify Files.Lines empty/missing file behavior

@gjenkins8 gjenkins8 added the v3.x Issues and Pull Requests related to the major version v3 label Jul 27, 2026

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@TerryHowe TerryHowe added the Has One Approval This PR has one approval. It still needs a second approval to be merged. label Jul 28, 2026
@TerryHowe
TerryHowe merged commit 14f349e into helm:dev-v3 Jul 28, 2026
5 checks passed
@scottrigby scottrigby added bug Categorizes issue or PR as related to a bug. and removed Has One Approval This PR has one approval. It still needs a second approval to be merged. labels Aug 13, 2026
@scottrigby scottrigby added this to the 3.21.4 milestone Aug 13, 2026
@scottrigby scottrigby added needs-pick Indicates that a PR needs to be cherry-picked into the next release candidate. picked Indicates that a PR has been cherry-picked into the next release candidate. and removed needs-pick Indicates that a PR needs to be cherry-picked into the next release candidate. labels Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Categorizes issue or PR as related to a bug. picked Indicates that a PR has been cherry-picked into the next release candidate. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. v3.x Issues and Pull Requests related to the major version v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants