1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fixed a bug in JSON decoding with Yaml backend, where a combination of dates, escaped or unicode encoded data and arrays would make the parser fail with a ParseError exception. [#2831 state:resolved]

Signed-off-by: Yehuda Katz <wycats@gmail.com>
This commit is contained in:
Bas Van Klinkenberg 2009-08-01 02:24:40 +02:00 committed by Yehuda Katz
parent d0301e13f4
commit 0fbeaa98e4
2 changed files with 17 additions and 8 deletions

View file

@ -34,11 +34,9 @@ module ActiveSupport
pos = scanner.pos pos = scanner.pos
elsif quoting == char elsif quoting == char
if json[pos..scanner.pos-2] =~ DATE_REGEX if json[pos..scanner.pos-2] =~ DATE_REGEX
# found a date, track the exact positions of the quotes so we can remove them later. # found a date, track the exact positions of the quotes so we can
# oh, and increment them for each current mark, each one is an extra padded space that bumps # overwrite them with spaces later.
# the position in the final YAML output times << pos << scanner.pos
total_marks = marks.size
times << pos+total_marks << scanner.pos+total_marks
end end
quoting = false quoting = false
end end
@ -64,7 +62,12 @@ module ActiveSupport
output = [] output = []
left_pos.each_with_index do |left, i| left_pos.each_with_index do |left, i|
scanner.pos = left.succ scanner.pos = left.succ
output << scanner.peek(right_pos[i] - scanner.pos + 1).gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do chunk = scanner.peek(right_pos[i] - scanner.pos + 1)
# overwrite the quotes found around the dates with spaces
while times.size > 0 && times[0] <= right_pos[i]
chunk[times.shift - scanner.pos - 1] = ' '
end
chunk.gsub!(/\\([\\\/]|u[[:xdigit:]]{4})/) do
ustr = $1 ustr = $1
if ustr.start_with?('u') if ustr.start_with?('u')
[ustr[1..-1].to_i(16)].pack("U") [ustr[1..-1].to_i(16)].pack("U")
@ -74,10 +77,10 @@ module ActiveSupport
ustr ustr
end end
end end
output << chunk
end end
output = output * " " output = output * " "
times.each { |i| output[i-1] = ' ' }
output.gsub!(/\\\//, '/') output.gsub!(/\\\//, '/')
output output
end end

View file

@ -33,7 +33,13 @@ class TestJSONDecoding < ActiveSupport::TestCase
%q({"a": "\u003cunicode\u0020escape\u003e"}) => {"a" => "<unicode escape>"}, %q({"a": "\u003cunicode\u0020escape\u003e"}) => {"a" => "<unicode escape>"},
%q({"a": "\\\\u0020skip double backslashes"}) => {"a" => "\\u0020skip double backslashes"}, %q({"a": "\\\\u0020skip double backslashes"}) => {"a" => "\\u0020skip double backslashes"},
%q({"a": "\u003cbr /\u003e"}) => {'a' => "<br />"}, %q({"a": "\u003cbr /\u003e"}) => {'a' => "<br />"},
%q({"b":["\u003ci\u003e","\u003cb\u003e","\u003cu\u003e"]}) => {'b' => ["<i>","<b>","<u>"]} %q({"b":["\u003ci\u003e","\u003cb\u003e","\u003cu\u003e"]}) => {'b' => ["<i>","<b>","<u>"]},
# test combination of dates and escaped or unicode encoded data in arrays
%q([{"d":"1970-01-01", "s":"\u0020escape"},{"d":"1970-01-01", "s":"\u0020escape"}]) =>
[{'d' => Date.new(1970, 1, 1), 's' => ' escape'},{'d' => Date.new(1970, 1, 1), 's' => ' escape'}],
%q([{"d":"1970-01-01","s":"http:\/\/example.com"},{"d":"1970-01-01","s":"http:\/\/example.com"}]) =>
[{'d' => Date.new(1970, 1, 1), 's' => 'http://example.com'},
{'d' => Date.new(1970, 1, 1), 's' => 'http://example.com'}]
} }
# load the default JSON backend # load the default JSON backend