Merge pull request #1093 from Shopify/bytesize-not-length

use bytesize, not length
This commit is contained in:
Florian Weingarten
2019-04-18 18:39:21 +01:00
committed by GitHub
4 changed files with 21 additions and 3 deletions

View File

@@ -115,7 +115,7 @@ module Liquid
end
def check_resources(context, node_output)
context.resource_limits.render_length += node_output.length
context.resource_limits.render_length += node_output.bytesize
return unless context.resource_limits.reached?
raise MemoryError.new("Memory limits exceeded".freeze)
end

View File

@@ -37,7 +37,7 @@ module Liquid
def assign_score_of(val)
if val.instance_of?(String)
val.length
val.bytesize
elsif val.instance_of?(Array) || val.instance_of?(Hash)
sum = 1
# Uses #each to avoid extra allocations.

View File

@@ -25,7 +25,7 @@ module Liquid
def render(context)
output = super
context.scopes.last[@to] = output
context.resource_limits.assign_score += output.length
context.resource_limits.assign_score += output.bytesize
''.freeze
end

View File

@@ -139,6 +139,16 @@ class TemplateTest < Minitest::Test
refute_nil t.resource_limits.assign_score
end
def test_resource_limits_assign_score_counts_bytes_not_characters
t = Template.parse("{% assign foo = 'すごい' %}")
t.render
assert_equal 9, t.resource_limits.assign_score
t = Template.parse("{% capture foo %}すごい{% endcapture %}")
t.render
assert_equal 9, t.resource_limits.assign_score
end
def test_resource_limits_assign_score_nested
t = Template.parse("{% assign foo = 'aaaa' | reverse %}")
@@ -187,6 +197,14 @@ class TemplateTest < Minitest::Test
assert_equal "ababab", t.render
end
def test_render_length_uses_number_of_bytes_not_characters
t = Template.parse("{% if true %}すごい{% endif %}")
t.resource_limits.render_length_limit = 10
assert_equal "Liquid error: Memory limits exceeded", t.render
t.resource_limits.render_length_limit = 18
assert_equal "すごい", t.render
end
def test_default_resource_limits_unaffected_by_render_with_context
context = Context.new
t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")