Bug #1311

YAML::dump creates strings YAML::load cannot parse

Added by rampion (Noah Easterly) over 10 years ago. Updated over 8 years ago.

Target version:
ruby -v:
ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.9.0], ruby 1.8.7 (2008-05-31 patchlevel 0) [i686-darwin9.3.0]


This case seems to occur whenever

  • the string begins with a newline, and
  • the last line doesn't end in whitespace (except, perhaps, newlines), and
  • the second line (after the initial whitespace) begins with more whitespace than some later line

Here are a couple example strings:

  • "\n a\nb"
  • "\n a\n b\nc"
  • "\n a\n b"

YAML::load throws an ArgumentError with a syntax error on the line that begins with less leading

  • YAML.load YAML.dump("\n a\nb") #=> ArgumentError: syntax error on line 3, col 0: `b'
  • YAML.load YAML.dump("\n a\n b\nc") #=> ArgumentError: syntax error on line 4, col 0: `c'
  • YAML.load YAML.dump("\n a\n b") #=> ArgumentError: syntax error on line 3, col 1: ` b'

This has been observed in ruby-1.8.6p0 and ruby-1.8.7p0.

Here is another observation of this same bug:


syck_scan_scalar.patch (497 Bytes) syck_scan_scalar.patch daz (Dave B), 03/25/2009 09:54 PM
test_yaml.patch (1.32 KB) test_yaml.patch all tests pass daz (Dave B), 03/25/2009 09:54 PM
test_yaml2.patch (1.42 KB) test_yaml2.patch coatl (caleb clausen), 09/30/2009 02:23 AM
syck_scan_scalar2.patch (540 Bytes) syck_scan_scalar2.patch coatl (caleb clausen), 09/30/2009 02:23 AM
yaml-error.txt (1.43 KB) yaml-error.txt weppos (Simone Carletti), 03/19/2010 09:02 PM

Related issues

Is duplicate of Ruby master - Bug #2767: YAML bugClosed02/20/2010Actions



Updated by rampion (Noah Easterly) over 10 years ago

Correction: should read "the second line (after the initial newline)"



Updated by daz (Dave B) over 10 years ago

Good reports, thanks.

I've found a workaround but I'm not sure enough about the YAML spec to guarantee that it's the correct fix.

The patch is mild - it just forces one style over another in the same way as the existing code and allows the new tests to pass along with the rest.

Two patches to 1.9.2dev - these files don't change often ;)

?CHANGELOG: Wed Mar 25 20:06:10 2009 Dave B
* ext/syck/emitter.c: Set SCAN_WHITEEDGE flag when
scalar begins with newline [ruby-core:23006][Bug #1311]

        * test/yaml/test_yaml.rb: add new tests and comment old
          Test::Unit::TestSuite code to enable them to run as $0



Updated by coatl (caleb clausen) about 10 years ago

I have also been bitten by this same bug. In addition to the cases described above, this problem also occurs when the input contains just a single newline (in either unix or dos style). The patch supplied by Dave B fixes the case of a string containing just a single unix newline, but fails to fix a string with just a single dos newline (and probably the other cases initially reported, if the unix newlines are converted to dos newlines). I'm attaching slightly a slightly modified patches, which addresses the dos newline issue, and tests single dos and unix newlines.

This problem also occurs in ruby 1.9.


Updated by daz (Dave B) about 10 years ago


Caleb's revisions are the way to go.

( test_yaml2.patch & syck_scan_scalar2.patch )

Harmlessly beneficial - no compatibility issues - emits YAML to the spec.




Updated by weppos (Simone Carletti) over 9 years ago

Here's an other test case for a similar issue.

require 'yaml'

c ="yaml-error.txt")
'rgumentError: syntax error on line 34, col 0: --
from /Users/weppos/.rvm/ruby-1.8.7-p248/lib/ruby/1.8/yaml.rb:133:in
from /Users/weppos/.rvm/ruby-1.8.7-p248/lib/ruby/1.8/yaml.rb:133:in `load'
from (irb):3


Updated by mame (Yusuke Endoh) over 9 years ago

  • Status changed from Open to Closed


Syck is deprecated, in effect. This issue won't be fixed.
Instead, try Psych. [ruby-core:29337]

Yusuke Endoh


Updated by coatl (caleb clausen) over 9 years ago

Yusuke Endoh and I have verified that all of the test cases reported here are fixed when using psych. For proof of most, see [ruby-core:29323]. And I just now verified the last test case is now ok, submitted by Simone Carletti. (No console dump, sorry.)


Updated by nobu (Nobuyoshi Nakada) over 9 years ago

  • Status changed from Closed to Open




Updated by nobu (Nobuyoshi Nakada) over 9 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r27591.
Noah, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


Also available in: Atom PDF