Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveSupport::JSON.decode dies on double escaped string

#2273

Parsing a long file with ActiveSupport::JSON.decode dies with ActiveSupport::JSON::ParseError: Invalid JSON string. The underlying error is an ArgumentError. Running the decode with the attached file yields the error.

I believe the date extraction places the wrong marks or uses them prematurely. Inspecting the error showed: '... "ranAt": "2009-02-19 21:48:05" "encoding": "UTF-8 , ...'. This is clearly wrong with a missing comma and the UTF-8 string not correctly closed.

Reported by Harm Aarts · March 17th, 2009 @ 03:34 PM

State: duplicate
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Harm Aarts
    Harm Aarts

    I believe the marks are counted wrong in the file activesupport-2.3.2/lib/active_support/json/decoding.rb line 41. The datetime field contains colons (:) and these are incorrectly counted. The same holds true for fields holding urls (http://).

    March 17th, 2009 @ 04:26 PM

  2. Harm Aarts
    Harm Aarts
    • Title changed from ActiveSupport::JSON.decode dies on long file to ActiveSupport::JSON.decode dies on double escaped string

    Say the content of a file is this: {"a":"\/\/\/\/\/","b":"2009-03-17 14:20:42",some_key Then reading that string with File.open("file_name").read yields this string: "{"a":"\/\/\/\/\/","b":"2009-03-17 14:20:42",some_key Decoding this string with ActiveSupport::JSON.decode goes wrong, the resulting hash: {"a"=>"/////", "b"=>"2009 03-17 14:20:42", "s me_key"=>"some_value"}

    Notice the hyphen between 2009-03 is missing as well as the 'o' in the 'some_key' word.

    I believe this is due to the fact that the string read from the file is double escaped. The in combination with the date matching causes the wrong characters to be replaced with a space which in some cases leads to a mangled result and in other cases in invalid JSON strings.

    A quick fix is to replace the '\/' sequences in the string which resulted from the file read with '/'. Example: ActiveSupport::JSON.decode(File.open("some_file").gsub!('\/','/'))

    I still believe this is a bug.

    March 18th, 2009 @ 10:24 AM

  3. okkez
    okkez

    I think this is a bug, too.

    I found reproduceable code, but it uses multibyte characters.

    >> h = { "data" => [{"key" => "漢字", "date" => "2008-01-01"}, {"key" => "あいう", "date" => "2009-01-01", "date2" => "2009-01-02"}]} >> ActiveSupport::JSON.decode(h.to_json) #=> {"data"=>[{"date"=>Tue, 01 Jan 2008, "key"=>"漢字"}, {"k y"=>"あいう", "date"=>"2009- 1-01", "d te2"=>"2009- 1-02"}]}

    I think this is a bug in ActiveSupport::JSON#convert_json_to_yaml.

    In convert_json_to_yaml, the local variable named 'times' is constructed in wrong timing.

    March 20th, 2009 @ 08:40 AM

  4. Bas van Klinkenberg
    Bas van Klinkenberg

    Please have a look at #2831, which is very similar to this ticket. I submitted a patch to #2831 which should solve this ticket as well.

    August 1st, 2009 @ 01:43 AM

  5. Michael Koziarski
    Michael Koziarski
    • Tag changed from date, json to bugmash, date, json

    August 3rd, 2009 @ 05:59 AM

  6. Nikolay Petrachkov
    Nikolay Petrachkov
    • Assigned user set to Michael Koziarski

    bug by Harm Aarts is "not reproducible" .

    Can reproduce bug by okkez

    Before patch by Bas van Klinkenberg (https://rails.lighthouseapp.com/projects/8994/tickets/2831)

    h = { "data" => [{"key" => "漢字", "date" => "2008-01-01"}, {"key" => "あいう", "date" => "2009-01-01", "date2" => "2009-01-02"}]}
    => {"data"=>[{"key"=>"漢字", "date"=>"2008-01-01"}, {"key"=>"あいう", "date"=>"2009-01-01", "date2"=>"2009-01-02"}]}
    >> ActiveSupport::JSON.decode(h.to_json)
    IndexError: index 118 out of string
        from /home/jastix/projects/bugmash/bookclub/vendor/rails/activesupport/lib/active_support/json/backends/yaml.rb:77:in `[]='
        from /home/jastix/projects/bugmash/bookclub/vendor/rails/activesupport/lib/active_support/json/backends/yaml.rb:77:in `block in convert_json_to_yaml'
        from /home/jastix/projects/bugmash/bookclub/vendor/rails/activesupport/lib/active_support/json/backends/yaml.rb:77:in `each'
        from /home/jastix/projects/bugmash/bookclub/vendor/rails/activesupport/lib/active_support/json/backends/yaml.rb:77:in `convert_json_to_yaml'
        from /home/jastix/projects/bugmash/bookclub/vendor/rails/activesupport/lib/active_support/json/backends/yaml.rb:16:in `decode'
        from /home/jastix/projects/bugmash/bookclub/vendor/rails/activesupport/lib/active_support/json/decoding.rb:11:in `decode'
        from (irb):45
        from /usr/local/bin/irb:12:in `'
    
    

    After patch:

    h = { "data" => [{"key" => "漢字", "date" => "2008-01-01"}, {"key" => "あいう", "date" => "2009-01-01", "date2" => "2009-01-02"}]}
    => {"data"=>[{"key"=>"漢字", "date"=>"2008-01-01"}, {"key"=>"あいう", "date"=>"2009-01-01", "date2"=>"2009-01-02"}]}
    
    

    ActiveSupport::JSON.decode(h.to_json) => {"data"=>[{"key"=>"漢字", "date"=>Tue, 01 Jan 2008}, {"key"=>"あいう", "date"=>Thu, 01 Jan 2009, "date2"=>Fri, 02 Jan 2009}]}

    
    

    August 8th, 2009 @ 01:34 PM

  7. Rizwan Reza
    Rizwan Reza

    not reproducible

    -1 I wasn't able to reproduce the error.

    August 8th, 2009 @ 10:51 PM

  8. Dan Croak
    Dan Croak

    Should be marked as "resolved, duplicate". Fix merged in by Yehuda in [#2831].

    August 8th, 2009 @ 11:00 PM

  9. Dan Croak
    Dan Croak

    uhhh... hardbap told me to say this is "verified"

    August 8th, 2009 @ 11:02 PM

  10. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to duplicate

    Duplicate of #2831

    August 8th, 2009 @ 11:33 PM

  11. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from bugmash, date, json to date, json

    August 8th, 2009 @ 11:34 PM