YAML Bug - can't round-trip a hash value with leading newlines

YAML can't round-trip the hash: {"default"=>"\r\n Monkey \r\nX"}.

Shouldn't yaml use the escaped string format for any string that starts with whitespace, ie,

yaml = "--- \ndefault: \"\\r\\n Monkey \\r\\nX\""
p YAML.load(yaml)
=> {"default"=>"\r\n Monkey \r\nX"}

Below is a demonstration of this:

ruby <<-EOF
hash = {"default"=>"\r\n Monkey \r\nX"}
yaml = YAML.dump(hash)
p yaml
hash2 = YAML.load(yaml)
EOF
"--- \ndefault: |-\n \r\n Monkey\r\n \r\n <h2>Contact us</h2>\n"
/usr/lib/ruby/1.8/yaml.rb:133:in `load': syntax error on line 4, col 2: ` X' (ArgumentError)
        from /usr/lib/ruby/1.8/yaml.rb:133:in `load'
        from -:4

ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]

Same bug exists in the jruby implementation as well.

iianm, it fails for ruby ver below 1.9.2. it is fixed ruby1.9.2

botp@jedi2:~
$ for i in `rvm list strings`; do rvm $i; ruby -v -ryaml test.rb; done
ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-linux]
"--- \ndefault: |-\n \r\n Monkey \r\n X\n"
/disk2_2/home/botp/.rvm/rubies/ruby-1.8.6-p399/lib/ruby/1.8/yaml.rb:133:in
`load': syntax error on line 4, col 2: ` X' (ArgumentError)
  from /disk2_2/home/botp/.rvm/rubies/ruby-1.8.6-p399/lib/ruby/1.8/yaml.rb:133:in
`load'
  from test.rb:4
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux]
"--- \ndefault: |-\n \r\n Monkey \r\n X\n"
/disk2_2/home/botp/.rvm/rubies/ruby-1.8.7-p174/lib/ruby/1.8/yaml.rb:133:in
`load': syntax error on line 4, col 2: ` X' (ArgumentError)
  from /disk2_2/home/botp/.rvm/rubies/ruby-1.8.7-p174/lib/ruby/1.8/yaml.rb:133:in
`load'
  from test.rb:4
ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]
"--- \ndefault: |-\n \r\n Monkey \r\n X\n"
/disk2_2/home/botp/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/yaml.rb:133:in
`load': syntax error on line 4, col 2: ` X' (ArgumentError)
  from /disk2_2/home/botp/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/yaml.rb:133:in
`load'
  from test.rb:4
ruby 1.9.1p378 (2010-01-10 revision 26273) [i686-linux]
"--- \ndefault: |-\n \r\n Monkey \r\n X\n"
/disk2_2/home/botp/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/yaml.rb:133:in
`load': syntax error on line 4, col 2: ` X' (ArgumentError)
  from /disk2_2/home/botp/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/yaml.rb:133:in
`load'
  from test.rb:4:in `<main>'

ruby 1.9.2dev (2010-05-31 revision 28117) [i686-linux]
"--- \ndefault: \"\\r\\n Monkey \\r\\n\\\n X\"\n"
ruby 1.9.2dev (2010-07-02 revision 28524) [i686-linux]
"--- \ndefault: \"\\r\\n Monkey \\r\\n\\\n X\"\n"
ruby 1.9.2dev (2010-07-11 revision 28618) [i686-linux]
"--- \ndefault: \"\\r\\n Monkey \\r\\n\\\n X\"\n"

botp@jedi2:~

best regards -botp

···

On Mon, Aug 16, 2010 at 1:14 PM, Daniel Sheppard <daniels@pronto.com.au> wrote:

ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]
Same bug exists in the jruby implementation as well.

Please file a bug if you could, at http://bugs.jruby.org :slight_smile:

···

On Mon, Aug 16, 2010 at 12:14 AM, Daniel Sheppard <daniels@pronto.com.au> wrote:

YAML can't round-trip the hash: {"default"=>"\r\n Monkey \r\nX"}.

Shouldn't yaml use the escaped string format for any string that starts with whitespace, ie,

yaml = "--- \ndefault: \"\\r\\n Monkey \\r\\nX\""
p YAML.load(yaml)
=> {"default"=>"\r\n Monkey \r\nX"}

Below is a demonstration of this:

ruby <<-EOF
hash = {"default"=>"\r\n Monkey \r\nX"}
yaml = YAML.dump(hash)
p yaml
hash2 = YAML.load(yaml)
EOF
"--- \ndefault: |-\n \r\n Monkey\r\n \r\n <h2>Contact us</h2>\n"
/usr/lib/ruby/1.8/yaml.rb:133:in `load': syntax error on line 4, col 2: ` X' (ArgumentError)
from /usr/lib/ruby/1.8/yaml.rb:133:in `load'
from -:4

ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]

Same bug exists in the jruby implementation as well.

This is bug #1311 in the ruby bug database:
  http://redmine.ruby-lang.org/issues/show/1311

The patch for it did finally get accepted in 1.9.2.

···

On 8/15/10, Daniel Sheppard <daniels@pronto.com.au> wrote:

YAML can't round-trip the hash: {"default"=>"\r\n Monkey \r\nX"}.

Shouldn't yaml use the escaped string format for any string that starts with
whitespace, ie,

yaml = "--- \ndefault: \"\\r\\n Monkey \\r\\nX\""
p YAML.load(yaml)
=> {"default"=>"\r\n Monkey \r\nX"}

Below is a demonstration of this:

ruby <<-EOF
hash = {"default"=>"\r\n Monkey \r\nX"}
yaml = YAML.dump(hash)
p yaml
hash2 = YAML.load(yaml)
EOF
"--- \ndefault: |-\n \r\n Monkey\r\n \r\n <h2>Contact us</h2>\n"
/usr/lib/ruby/1.8/yaml.rb:133:in `load': syntax error on line 4, col 2: `
X' (ArgumentError)
        from /usr/lib/ruby/1.8/yaml.rb:133:in `load'
        from -:4

ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]

Same bug exists in the jruby implementation as well.