Skip to content

Do not count a leading BOM as a column (#334) - #345

Open
youdie006 wants to merge 1 commit into
yaml:masterfrom
youdie006:fix/334-bom-column
Open

Do not count a leading BOM as a column (#334)#345
youdie006 wants to merge 1 commit into
yaml:masterfrom
youdie006:fix/334-bom-column

Conversation

@youdie006

Copy link
Copy Markdown

Fixes #334.

Problem

When the input encoding is set explicitly with yaml_parser_set_encoding() (e.g. YAML_UTF8_ENCODING), the reader's determine_encoding() BOM stripping is skipped, so a leading BOM reaches the scanner. yaml_parser_scan_to_next_token() skips it with SKIP(parser) — but SKIP increments parser->mark.column, so the first real token starts at column 1 instead of 0. A root-level block mapping then terminates at the first newline and parsing fails with did not find expected <document start>. Per YAML 1.2 section 5.2 a BOM is not content and must not advance the column. The same input parses correctly under YAML_ANY_ENCODING (where the BOM was already stripped upstream).

Fix

Reset mark.column to 0 after skipping the BOM, so the following token starts at column 0.

Test

Added tests/test-bom.c (registered in Makefile.am and CMakeLists.txt, following test-nesting): it parses "\xEF\xBB\xBF" "a: b\nc: d\n" under both YAML_UTF8_ENCODING and YAML_ANY_ENCODING and asserts both fully parse the two-entry mapping. Red-green verified: before the fix the explicit-UTF-8 case fails (did not find expected <document start>), after the fix both pass. The existing test-reader/test-nesting/test-version still pass, and parser-token output is byte-identical to the unmodified build across examples/*.yaml (the fix only affects the BOM-at-column-0 path).

When the input encoding is set explicitly with yaml_parser_set_encoding,
the reader does not strip a leading BOM, so the scanner skips it in
yaml_parser_scan_to_next_token. SKIP() advances mark.column, so the first
real token started at column 1 instead of 0, and a root-level block
mapping then failed to parse with "did not find expected <document
start>". Per YAML 1.2 section 5.2 a BOM is not content and must not
advance the column.

Reset mark.column to 0 after skipping the BOM. Add a regression test.

Fixes yaml#334
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leading BOM is counted as a column when the encoding is set explicitly

1 participant