Repository navigation
Report a truncated document as UnexpectedEofError instead of an unexpected "\x00" - #724
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
…ected "\x00"
When a document ends where the parser expects a character, parse() reports
the end-of-input sentinel as if it were a character of the document:
>>> tomlkit.parse("key = [1")
UnexpectedCharError: Unexpected character: '\x00' at line 1 col 0
Closing-bracket and comma positions already raise UnexpectedEofError, and
_parse_table() uses the "check end() first, then report the character"
pattern. Five sites were missing it: _parse_key_value(), _parse_value(),
_parse_array(), _parse_inline_table() and Source.consume() via
_parse_bool().
Keep the Source invariant: check positionally with end(), never compare to
Source.EOF, so a NUL byte really present in the document is still reported
as an unexpected character.
|
🛡️ Human Attestation required. A human needs to confirm this PR once before it can merge. Attest this PR — sign in with GitHub, pass the challenge, and confirm one of the statements. |
qianeric-backup
marked this pull request as ready for review
November 27, 2026 12:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a document ends where the parser expects a character,
parse()reports the end-of-input sentinel as if it were a character of the input:'\x00'(Source.EOF) is not in the document — it is the placeholder the parser holds past the end of the input, so the message points at a character that does not exist.Sourcedocuments this invariant itself:The same functions already do this correctly a few lines away — closing-bracket and comma positions raise
UnexpectedEofErrorwith the comment "EOF here is an issue", and_parse_table()is the exact pattern to copy:Five sites were missing that guard:
_parse_key_value()parse("key")_parse_value()parse("key = ")_parse_array()parse("key = [1")_parse_inline_table()parse("key = {a = 1")Source.consume()via_parse_bool()parse("key = tru")UnexpectedEofErroris already raised 17 times inparser.py; these five were the gaps.Changes
end()guard at those five sites, so a document that stops early raisesUnexpectedEofError("Unexpected end of file"), matching the cases that already work (parse("key = ["),parse("key = {"),parse('key = "a')).Sourceinvariant: the check is positional (self.end()), nevercurrent == Source.EOF, so a NUL byte genuinely present in the document is stillUnexpected character: '\x00'.UnexpectedEofErroruses.value()/key_value()variant.[Unreleased] / Fixed.Verification
On a clean tarball of
master(3cdcbde):pytest tests(minustest_toml_tests, which needs thetoml-testsubmodule)master+ new tests only7 failed, 805 passed— exactly the seven new casesmaster+ this patch812 passedcomm -3over the sortedFAILEDlines shows only the new test ids, so nothing else changed state.toml-testvalid documents render byte-identical after the patch, as do 16 ad-hoc API round-trips.array/no-close-01..08,array/no-close-table-01/02,array/no-comma-03,inline-table/no-close-02— each now reportsUnexpectedEofErrorinstead of the sentinel.ParseErroror anInternalParserErroron either tree.ruff checkandruff format --checkat0.17.6(the version pinned in.pre-commit-config.yaml): clean.Fixes #
Duplicate Check
_validate_childfor tables/AoT inside an array —items.pyonly) and Fix three toml-test 1.1.0 compliance gaps #587 (toml-test 1.1.0 compliance —_parse_numberand BOM handling); neither changes how an unexpected end of input is reported.Agent Drafting Metadata