Skip to content

Show the line and context for an error at the end of the input - #3122

Open
Kuriyeatsuchi wants to merge 3 commits into
stephenberry:mainfrom
Kuriyeatsuchi:format-error-context-at-end-of-input
Open

Kuriyeatsuchi wants to merge 3 commits into
stephenberry:mainfrom
Kuriyeatsuchi:format-error-context-at-end-of-input

Conversation

@Kuriyeatsuchi

@Kuriyeatsuchi Kuriyeatsuchi commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

glz::format_error printed only index N when the error position was the end of the buffer. That is where a truncated document or a missing closing bracket fails, so a common error came without a line, column or context:

index 31: expected_bracket

Now:

3:13: expected_bracket
     "b": [3, 4
               ^

The caret goes just past the last character that is not trailing whitespace, so a file that ends in a newline gets the same message. The reported index is unchanged, and input that is empty or only whitespace still gets index N.

This also covers the "error does not contain line and context info" part of #3067, where the YAML reader reports object: 42 at the end of the input.

Other changes to the context:

  • An error at a newline character printed the next line as part of the context, which put the caret under the wrong text. The context now ends at that newline.
  • The carriage return of a CRLF line ending is left out of the context.
  • Binary input (BEVE, CBOR, MessagePack, BSON) held in a std::string reaches the same text path. A line that contains control characters other than tab and CR is treated as binary, and only the index is reported, so a truncated binary buffer still gives index N: unexpected_end instead of printing raw bytes. The byte at the error is exempt: a raw control character in a JSON string is the error itself, so that message keeps its context and shows the character as a space.

Tests:

  • json_test covers errors at the end of the input, with trailing whitespace and on a CRLF line, an error at a newline, whitespace-only input, a control character at the error, and binary buffers.
  • beve_test and cbor_test check that a truncated buffer gives only the index. The CBOR buffer contains no zero bytes.
  • The YAML missing key tests pinned the index-only output, so they now expect the line, column and context.

Kuriyeatsuchi and others added 3 commits November 22, 2026 02:04
format_error printed only "index N" when the error position was the end
of the buffer. That is where a truncated document or a missing closing
bracket fails, and where the YAML reader reports some type mismatches
(issue stephenberry#3067), so these common errors came without a line, column or
context. Point just past the last character that is not trailing
whitespace instead, so the message shows the line where the input
stops. The reported index is unchanged, and input that is empty or only
whitespace still gets "index N".

An error at a newline character also printed the following line as part
of its context, which put the caret under the wrong text. The context now
ends at that newline.

The YAML missing key tests pinned the index-only output and now expect
the line, column and context.
With GLZ_NULL_TERMINATED=false the reader reports unexpected_end for
these inputs instead of expected_comma or expected_bracket, so the
json_test_non_null build failed on the error code. The tests are about
the line, column and context, so check those and not the code.
@stephenberry

Copy link
Copy Markdown
Owner

Thanks for this! I pushed 7e9f50b on top of it with these changes:

  • Binary buffers report only the index again. BEVE, CBOR, MessagePack and BSON data is often held in a std::string, so it goes through the same text path. A truncated binary buffer fails at the end, and with this PR it printed the raw bytes (NULs and control characters included) with a meaningless line and column, where main gave index N: unexpected_end. Now a line that contains control characters other than tab and CR is treated as binary and gets only the index. This also covers errors just before the end of a binary buffer, which printed raw bytes on main too. The check doesn't rely on zero bytes, since small CBOR and MessagePack values have none.
  • The byte at the error is exempt. A raw control character in a JSON string is itself the error, so that message keeps its context, with the character shown as a space so the caret still lines up.
  • CRLF: the trailing \r of a CRLF line is left out of the context. More errors show context now, so it came up more often.
  • Tests: json_test covers an error at the end of a CRLF line, a control character at the error, and a binary buffer with errors at the end and in the middle. beve_test and cbor_test check that a truncated buffer gives only the index.

I also updated the description to match. The build-direct-clang-cl failure is unrelated and was fixed on main by #3124. A rebase will clear it.

@Kuriyeatsuchi

Copy link
Copy Markdown
Contributor Author

Thanks for the fixes, and for catching the binary buffer case!

This branch has not been deployed

No deployments
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.

2 participants