Skip to content

Report a truncated document as UnexpectedEofError instead of an unexpected "\x00" - #724

Open
qianeric-backup wants to merge 1 commit into
python-poetry:masterfrom
qianeric-backup:unexpected-eof-instead-of-sentinel
Open

qianeric-backup wants to merge 1 commit into
python-poetry:masterfrom
qianeric-backup:unexpected-eof-instead-of-sentinel

Conversation

@qianeric-backup

Copy link
Copy Markdown

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:

>>> tomlkit.parse("key = [1")
tomlkit.exceptions.UnexpectedCharError: Unexpected character: '\x00' at line 1 col 0

'\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. Source documents this invariant itself:

EOF is a placeholder value … detected positionally (end() / _idx >= len), never by comparing to this value, so a real NUL byte in the input is not mistaken for EOF.

The same functions already do this correctly a few lines away — closing-bracket and comma positions raise UnexpectedEofError with the comment "EOF here is an issue", and _parse_table() is the exact pattern to copy:

        if self.end():
            raise self.parse_error(UnexpectedEofError)
        elif self._current != "]":
            raise self.parse_error(UnexpectedCharError, self._current)

Five sites were missing that guard:

site input
_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")

UnexpectedEofError is already raised 17 times in parser.py; these five were the gaps.

Changes

  • Add the end() guard at those five sites, so a document that stops early raises UnexpectedEofError ("Unexpected end of file"), matching the cases that already work (parse("key = ["), parse("key = {"), parse('key = "a')).
  • Keep the Source invariant: the check is positional (self.end()), never current == Source.EOF, so a NUL byte genuinely present in the document is still Unexpected character: '\x00'.
  • No change to line/column computation: the EOF positions keep the convention every other UnexpectedEofError uses.
  • Tests: a parametrized regression test over the five sites, an inverse test that a real NUL byte is still an unexpected character, and a value() / key_value() variant.
  • CHANGELOG entry under [Unreleased] / Fixed.

Verification

On a clean tarball of master (3cdcbde):

tree pytest tests (minus test_toml_tests, which needs the toml-test submodule)
master + new tests only 7 failed, 805 passed — exactly the seven new cases
master + this patch 812 passed

comm -3 over the sorted FAILED lines shows only the new test ids, so nothing else changed state.

  • Valid-input equivalence: 262 toml-test valid documents render byte-identical after the patch, as do 16 ad-hoc API round-trips.
  • Of 483 invalid documents, only 12 change, and they are the unclosed-array / unclosed-inline-table cases: array/no-close-01..08, array/no-close-table-01/02, array/no-comma-03, inline-table/no-close-02 — each now reports UnexpectedEofError instead of the sentinel.
  • Truncation sweep: 32,921 truncated prefixes of the valid corpus, parsed before and after. Before, the sentinel leaked from these five sites 9,992 times; after, zero. No truncation produces a non-ParseError or an InternalParserError on either tree.
  • ruff check and ruff format --check at 0.17.6 (the version pinned in .pre-commit-config.yaml): clean.

Fixes #

Duplicate Check

Agent Drafting Metadata

  • Agent: AI assistant, under human supervision.
  • Model: see session metadata.
  • Notes: the agent reproduced the defect, compared it with the sibling code paths, wrote the patch, and ran the test / lint / invariance checks. The human reviewed the full diff and signed off below.

…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

Copy link
Copy Markdown

🛡️ 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
qianeric-backup marked this pull request as ready for review November 27, 2026 12:07
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.

1 participant