Skip to content

fix(eval): return error for out-of-range slice expression instead of panicking#232

Open
SAY-5 wants to merge 1 commit into
CloudyKit:masterfrom
SAY-5:fix/slice-expr-out-of-range
Open

fix(eval): return error for out-of-range slice expression instead of panicking#232
SAY-5 wants to merge 1 commit into
CloudyKit:masterfrom
SAY-5:fix/slice-expr-out-of-range

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented May 24, 2026

Slice expressions like {{ .[1:10] }} on a shorter slice pass the user-controlled bounds straight into reflect.Value.Slice, which panics with a runtime.Error ("slice index out of bounds"). The runtime recover in (*Runtime).recover re-panics on runtime.Error, so this crashes the calling application instead of being returned as a normal template error.

The index path (indexArg) already validates bounds and returns an error; this brings slice expressions in line by validating index/length against the base length before calling Slice.

Reproduces with {{ .[1:10] }}, {{ .[2:1] }}, and {{ .[-1:1] }}.

Added TestEvalSliceExpressionOutOfRange covering over-length, reversed, and negative bounds. It panics without the fix and passes with it. Full suite green under go test -race ./....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant