From f8eb90564c46d8643afe9db7b051f5d0ca4b6a00 Mon Sep 17 00:00:00 2001 From: Joseph Axisa Date: Sat, 17 Oct 2026 22:15:55 -0700 Subject: [PATCH] Accept a tab after the indentation of a block scalar's first line When a block scalar's indentation is auto-detected, a first content line made of the indentation spaces followed by a tab was rejected with "found a tab character where an indentation space is expected". YAML 1.2.2 counts only spaces as indentation, so the tab is content (example 8.2, yaml-test-suite R4YG, 96NN and Y79Y/001). A tab inside the parent's indentation is still an error. Same change as yaml/go-yaml 85bfc37 ("Preserve tab content after block indentation", merged in yaml/go-yaml#440). go-yaml v4 counts columns from 1 and parser.indent the same way, so the condition carries over unchanged. --- decode_test.go | 13 +++++++++++++ scannerc.go | 8 ++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/decode_test.go b/decode_test.go index 5f7326c1..94bc0250 100644 --- a/decode_test.go +++ b/decode_test.go @@ -250,6 +250,17 @@ var unmarshalTests = []struct { map[string]string{"scalar": "\nfolded line\nnext line\n * one\n * two\n\nlast line\n"}, }, + // Block scalar whose first line starts with a tab after the indentation: + // the tab is content (YAML 1.2.2 example 8.2). + { + "- |\n detected\n- >\n \n \n # detected\n- |1\n explicit\n- >\n \t\n detected\n", + []interface{}{"detected\n", "\n\n# detected\n", " explicit\n", "\t\ndetected\n"}, + }, + { + "a: |-\n \ttext\n", + map[string]string{"a": "\ttext"}, + }, + // Map inside interface with no type hints. { "a: {b: c}", @@ -1028,6 +1039,8 @@ var unmarshalErrorTests = []struct { {"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`}, {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"}, {"a:\n 1:\nb\n 2:", ".*could not find expected ':'"}, + {"a:\n b: |\n \ttext\n", "yaml: line 2: found a tab character where an indentation space is expected"}, + {"- |\n\ttext\n", "yaml: line 2: found a tab character where an indentation space is expected"}, {"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"}, {"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665 {"0: [:!00 \xef", "yaml: incomplete UTF-8 octet sequence"}, // Issue #666 diff --git a/scannerc.go b/scannerc.go index 7ee4f10e..c78c4b3e 100644 --- a/scannerc.go +++ b/scannerc.go @@ -2405,8 +2405,12 @@ func yaml_parser_scan_block_scalar_breaks(parser *yaml_parser_t, indent *int, br max_indent = parser.mark.column } - // Check for a tab character messing the indentation. - if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) { + // Check for a tab character messing the indentation. Before the + // indentation is detected, a tab after the required indentation + // prefix is content, not indentation (YAML 1.2.2 example 8.2). + if is_tab(parser.buffer, parser.buffer_pos) && + ((*indent == 0 && parser.mark.column <= parser.indent) || + (*indent > 0 && parser.mark.column < *indent)) { return yaml_parser_set_scanner_error(parser, "while scanning a block scalar", start_mark, "found a tab character where an indentation space is expected") }