Skip to content
Snippets Groups Projects
Commit 600de460 authored by Nick Zavaritsky's avatar Nick Zavaritsky Committed by Roman Tsisyk
Browse files

Fix #354: yaml incorrectly escapes special characters in a string

parent 09c0c240
No related branches found
No related tags found
No related merge requests found
......@@ -271,7 +271,7 @@ ok - ucdata
# compact: end
ok - compact
# output
1..8
1..9
ok - encode for true
ok - decode for 'yes'
ok - encode for false
......@@ -279,6 +279,7 @@ ok - compact
ok - encode for nil
ok - decode for ~
ok - encode for binary
ok - encode for binary (2) - gh-354
ok - tutorial string
# output: end
ok - output
......
......@@ -42,7 +42,7 @@ local function test_compact(test, s)
end
local function test_output(test, s)
test:plan(8)
test:plan(9)
test:is(s.encode({true}), '---\n- true\n...\n', "encode for true")
test:is(s.decode("---\nyes\n..."), true, "decode for 'yes'")
test:is(s.encode({false}), '---\n- false\n...\n', "encode for false")
......@@ -51,6 +51,8 @@ local function test_output(test, s)
test:is(s.decode("---\n~\n..."), s.NULL, "decode for ~")
test:is(s.encode("\x80\x92\xe8s\x16"), '--- !!binary gJLocxY=\n...\n',
"encode for binary")
test:is(s.encode("\x08\x5c\xc2\x80\x12\x2f"), '--- !!binary CFzCgBIv\n...\n',
"encode for binary (2) - gh-354")
test:is(s.encode("Tutorial -- Header\n====\n\nText"),
"--- |-\n Tutorial -- Header\n ====\n\n Text\n...\n", "tutorial string");
end
......
......@@ -44,6 +44,7 @@ extern "C" {
#include <lj_state.h>
#include "yaml.h"
#include "yaml_private.h"
#include "b64.h"
} /* extern "C" */
#include "lua/utils.h"
......@@ -515,8 +516,24 @@ static yaml_scalar_style_t analyze_string(struct lua_yaml_dumper *dumper,
return YAML_PLAIN_SCALAR_STYLE;
}
/* Truncated UTF-8 sequence? */
if (p + continuation_bytes >= e) {
*is_binary = 1;
return YAML_PLAIN_SCALAR_STYLE;
}
/*
* Valid non-printable UTF-8? Encode as binary since otherwise
* the conversion may be lossy, ex: '\xc2\x80' -> '\x80'.
*/
yaml_string_t ys; ys.pointer = (yaml_char_t *)p;
if (!IS_PRINTABLE(ys)) {
*is_binary = 1;
return YAML_PLAIN_SCALAR_STYLE;
}
++p;
while (p < e && continuation_bytes > 0 && *p >= 0x80 && *p <= 0xBF) {
while (continuation_bytes > 0 && *p >= 0x80 && *p <= 0xBF) {
++p;
continuation_bytes -= 1;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment