Fix panic in slice filter with negative length - #149
Open
ChrisJr404 wants to merge 1 commit into
Open
Conversation
The slice filter computed end = start + length without a lower bound. A negative length produced end < start, so the runes[start:end] and slice[start:end] expressions panicked with a slice-bounds-out-of-range runtime error. Clamp end up to start so a negative length yields an empty result, matching Ruby's String#slice behavior, and add regression tests covering the string and array cases.
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
The
slicefilter panics withruntime error: slice bounds out of rangewhen given a negative length. This is reachable from the main entry points (Engine.ParseAndRender,Template.Render) purely through template source, so a template author can crash the renderer.Cause
slicecomputesend := start + lengthand only clampsendagainst the upper bound (len). It never clamps againststart. A negative length makesend < start, and the subsequentrunes[start:end]/slice[start:end]slice expression panics.Minimal reproductions (both panic before this change):
Fix
Clamp
endup tostartin both the string and array branches, so a negative length yields an empty result instead of panicking. This matches Ruby'sString#slice(start, length), which returnsnilfor a negative length. Existing valid slices (positive length, negative start, out-of-range values) are unaffected.Tests
Added regression cases to
filterTestscovering negative length for both a string (ASCII and multi-byte runes) and an array.make testpasses.Checklist
make testpasses.make lintpasses. (golangci-lint not run locally;go vet ./...is clean and the change is a two-line bounds clamp.)