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

D

Daniel Sheppard

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

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

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

Below is a demonstration of this:

ruby <<-EOF
hash =3D {"default"=3D>"\r\n Monkey \r\nX"}
yaml =3D YAML.dump(hash)
p yaml
hash2 =3D 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.
 
B

botp

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
 
C

Charles Oliver Nutter

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

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

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

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

Below is a demonstration of this:

ruby <<-EOF
hash =3D {"default"=3D>"\r\n =C2=A0Monkey \r\nX"}
yaml =3D YAML.dump(hash)
p yaml
hash2 =3D YAML.load(yaml)
EOF
"--- \ndefault: |-\n =C2=A0\r\n =C2=A0 =C2=A0Monkey\r\n =C2=A0\r\n =C2=A0=
/usr/lib/ruby/1.8/yaml.rb:133:in `load': syntax error on line 4, col 2: `= =C2=A0X' (ArgumentError)
=C2=A0 =C2=A0 =C2=A0 =C2=A0from /usr/lib/ruby/1.8/yaml.rb:133:in `load'
=C2=A0 =C2=A0 =C2=A0 =C2=A0from -:4

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


Same bug exists in the jruby implementation as well.
 
C

Caleb Clausen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top