ingydotnet / yaml-pm

YAML Perl Module
http://search.cpan.org/dist/YAML/
20 stars 26 forks source link

Dump() can create invalid YAML 1.1 and 1.2 #217

Open perlpunk opened 5 years ago

perlpunk commented 5 years ago

For certain strings YAML::Dump() creates YAML with control characters as a plain string:

use Encode;
use Devel::Peek;
use YAML ();
use YAML::XS ();
my $binary = "\342\202\254";
my $dump = YAML::Dump($binary);
Dump $dump;
my $encoded = encode_utf8($dump);
Dump $encoded;
YAML::XS::Load($encoded);

__END__

SV = PV(0x5609b8aea0e0) at 0x5609b8b092c8
  REFCNT = 1
  FLAGS = (POK,IsCOW,pPOK)
  PV = 0x5609b8cbc5a0 "--- \342\202\254\n"\0
  CUR = 8
  LEN = 10
  COW_REFCNT = 0
SV = PV(0x5609b8c16030) at 0x5609b8d0a0e0
  REFCNT = 1
  FLAGS = (POK,IsCOW,pPOK)
  PV = 0x5609b8e57930 "--- \303\242\302\202\302\254\n"\0
  CUR = 11
  LEN = 13
  COW_REFCNT = 0
YAML::XS::Load Error: The problem:

    control characters are not allowed

was found at document: 0

Strings with such characters should be quoted, and in YAML 1.1 they also need to be escaped.

https://yaml.org/spec/1.1/

[128]   nb-double-char  ::= ( nb-char - “\” - “"” ) | ns-esc-char   
[34]    nb-char ::= c-printable - b-char

https://yaml.org/spec/1.2/spec.html

[107]   nb-double-char  ::= c-ns-esc-char | ( nb-json - “\” - “"” )
[2] nb-json ::= #x9 | [#x20-#x10FFFF]

See also https://github.com/perlpunk/YAML-PP-p5/issues/17