Skip to content

fix: keep numeric filter arguments as long through bounds checks - #842

Merged
ebussieres merged 1 commit into
PebbleTemplates:masterfrom
princejain756:fix/numeric-args-long-overflow
Oct 19, 2026
Merged

ebussieres merged 1 commit into
PebbleTemplates:masterfrom
princejain756:fix/numeric-args-long-overflow

Conversation

@princejain756

@princejain756 princejain756 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #841.

split, abbreviate and slice read numeric arguments as Long but narrowed them with intValue() before the bounds checks, so values beyond the int range wrapped:

  • split clamps the limit to the int range, so a huge limit still means "all the parts"
  • abbreviate compares length as long, so a huge width returns the input unchanged and a wrapped negative is no longer possible
  • slice keeps fromIndex/toIndex as long through the checks and now also validates fromIndex against the input size when toIndex is omitted (previously a raw StringIndexOutOfBoundsException escaped)

Regression tests cover one failing case per filter; SplitFilterTest and CoreFiltersTest pass 103/103.

I audited every intValue() call in the codebase for the same pattern. These three filters were the only real bugs:

callsite verdict
SplitFilter, AbbreviateFilter, SliceFilter fixed here
OperatorUtils integer path safe: only reached when both operands are already Integer/Short/Byte; Longs route to longOperation first
TypeUtils.compatibleCast intentional: narrows only when the callee declared int
MapResolver key cast intentional: narrows to the map's declared Integer key type
MapExpression capacity hint cannot overflow: Map.size() is already int
LazyLength/LazyRevIndex cannot overflow: list sizes are int-bounded
SpecializedWriter safe: guarded by an Integer cast first

split, abbreviate and slice narrowed their Long arguments with intValue() before validating, so values beyond the int range wrapped to unrelated numbers: split could leave the input unsplit, abbreviate could truncate a short string, and slice could return the wrong substring instead of failing.
@ebussieres ebussieres added this to the 4.1.3 milestone Oct 19, 2026
@ebussieres
ebussieres merged commit 36de15c into PebbleTemplates:master Oct 19, 2026
3 checks passed
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.

split, abbreviate and slice filters narrow numeric arguments to int before bounds checks

2 participants