From 68c1334335ae7b66f79015c1425775d72cab5f3a Mon Sep 17 00:00:00 2001 From: "Chris (ChrisJr404)" <11917633+ChrisJr404@users.noreply.github.com> Date: Tue, 18 Aug 2026 11:08:26 -0400 Subject: [PATCH] Fix panic in slice filter with negative length 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. --- filters/standard_filters.go | 6 ++++++ filters/standard_filters_test.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/filters/standard_filters.go b/filters/standard_filters.go index 13b2ca1..4737a46 100644 --- a/filters/standard_filters.go +++ b/filters/standard_filters.go @@ -391,6 +391,9 @@ func AddStandardFilters(fd FilterDictionary) { //nolint: gocyclo start = len(runes) } end := start + n + if end < start { + end = start + } if end > len(runes) { end = len(runes) } @@ -413,6 +416,9 @@ func AddStandardFilters(fd FilterDictionary) { //nolint: gocyclo start = len(slice) } end := start + n + if end < start { + end = start + } if end > len(slice) { end = len(slice) } diff --git a/filters/standard_filters_test.go b/filters/standard_filters_test.go index 9141c98..55d3591 100644 --- a/filters/standard_filters_test.go +++ b/filters/standard_filters_test.go @@ -169,6 +169,9 @@ Liquid" | slice: 2, 4`, "quid"}, {`"白鵬翔" | slice: -100`, "白"}, {`"白鵬翔" | slice: -100, 200`, "白鵬翔"}, {`">` + strings.Repeat(".", 10000) + `<" | slice: 1, 10000`, strings.Repeat(".", 10000)}, + {`"Liquid" | slice: 2, -1`, ""}, + {`"白鵬翔" | slice: 1, -1`, ""}, + {`"a,b,c" | split: "," | slice: 1, -1 | join`, ""}, {`"a,b,c" | split: "," | slice: -1 | join`, "c"}, {`"a,b,c" | split: "," | slice: 1, 1 | join`, "b"}, {`"a,b,c" | split: "," | slice: 0, 2 | join`, "a b"},