mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 15:15:19 -07:00
Avoid internal errors for large arguments to slice filter
Use saturating conversion, which has expected semantics for large integers.
This commit is contained in:
@@ -7,6 +7,13 @@ require 'bigdecimal'
|
|||||||
module Liquid
|
module Liquid
|
||||||
module StandardFilters
|
module StandardFilters
|
||||||
MAX_I32 = (1 << 31) - 1
|
MAX_I32 = (1 << 31) - 1
|
||||||
|
private_constant :MAX_I32
|
||||||
|
|
||||||
|
MIN_I64 = -(1 << 63)
|
||||||
|
MAX_I64 = (1 << 63) - 1
|
||||||
|
I64_RANGE = MIN_I64..MAX_I64
|
||||||
|
private_constant :MIN_I64, :MAX_I64, :I64_RANGE
|
||||||
|
|
||||||
HTML_ESCAPE = {
|
HTML_ESCAPE = {
|
||||||
'&' => '&',
|
'&' => '&',
|
||||||
'>' => '>',
|
'>' => '>',
|
||||||
@@ -186,10 +193,19 @@ module Liquid
|
|||||||
offset = Utils.to_integer(offset)
|
offset = Utils.to_integer(offset)
|
||||||
length = length ? Utils.to_integer(length) : 1
|
length = length ? Utils.to_integer(length) : 1
|
||||||
|
|
||||||
if input.is_a?(Array)
|
begin
|
||||||
input.slice(offset, length) || []
|
if input.is_a?(Array)
|
||||||
else
|
input.slice(offset, length) || []
|
||||||
input.to_s.slice(offset, length) || ''
|
else
|
||||||
|
input.to_s.slice(offset, length) || ''
|
||||||
|
end
|
||||||
|
rescue RangeError
|
||||||
|
if I64_RANGE.cover?(length) && I64_RANGE.cover?(offset)
|
||||||
|
raise # unexpected error
|
||||||
|
end
|
||||||
|
offset = offset.clamp(I64_RANGE)
|
||||||
|
length = length.clamp(I64_RANGE)
|
||||||
|
retry
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -109,6 +109,10 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_raises(Liquid::ArgumentError) do
|
assert_raises(Liquid::ArgumentError) do
|
||||||
@filters.slice('foobar', 0, "")
|
@filters.slice('foobar', 0, "")
|
||||||
end
|
end
|
||||||
|
assert_equal("", @filters.slice("foobar", 0, -(1 << 64)))
|
||||||
|
assert_equal("foobar", @filters.slice("foobar", 0, 1 << 63))
|
||||||
|
assert_equal("", @filters.slice("foobar", 1 << 63, 6))
|
||||||
|
assert_equal("", @filters.slice("foobar", -(1 << 63), 6))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_slice_on_arrays
|
def test_slice_on_arrays
|
||||||
@@ -123,6 +127,10 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal(%w(r), @filters.slice(input, -1))
|
assert_equal(%w(r), @filters.slice(input, -1))
|
||||||
assert_equal(%w(), @filters.slice(input, 100, 10))
|
assert_equal(%w(), @filters.slice(input, 100, 10))
|
||||||
assert_equal(%w(), @filters.slice(input, -100, 10))
|
assert_equal(%w(), @filters.slice(input, -100, 10))
|
||||||
|
assert_equal([], @filters.slice(input, 0, -(1 << 64)))
|
||||||
|
assert_equal(input, @filters.slice(input, 0, 1 << 63))
|
||||||
|
assert_equal([], @filters.slice(input, 1 << 63, 6))
|
||||||
|
assert_equal([], @filters.slice(input, -(1 << 63), 6))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_truncate
|
def test_truncate
|
||||||
|
|||||||
Reference in New Issue
Block a user