Skip to content

🐛 fix(encoding): reject encoding labels with NUL - #2274

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/encoding-label-nul
Nov 16, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/encoding-label-nul

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

An encoding= label that contains a NUL byte was accepted, and the label was matched as though the NUL were the end of the string. parse(b"<p>\xe9</p>", encoding="latin1\x00junk") decoded the document as windows-1252, even though the label names no encoding. The lookup now refuses such a label, so the call raises the same LookupError as any other unknown label. Fixes #2269.

The table search stopped at the NUL

The label lookup trims ASCII whitespace, lowercases the label into a C string, and searches the encoding table with C string comparisons. A NUL inside the label therefore ended the comparison early, and latin1 matched. Any label with a NUL, whether before, inside or after the name, now returns no entry.

The error message stopped at the NUL

The unknown-label error formatted the label with %s, so a rejected label showed only the text before its NUL, which read as though latin1 were unknown. The message now formats the full label.

Behavior changes

Labels without a NUL behave as before. A label with a NUL raises LookupError where it used to be accepted, and the message shows the full label text.

The encoding= label was matched as a C string in the table search, so
a label with an embedded NUL matched the table entry for the text before
the NUL. A label such as "latin1" followed by a NUL and more text was
accepted and decoded as windows-1252, although the label is not a valid
encoding name.

The lookup now refuses any label that contains a NUL, and the LookupError
shows the whole label instead of the text before the NUL.

Fixes tox-dev#2269
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat merged commit 3cf251e into tox-dev:main Nov 16, 2026
1 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Encoding labels with a NUL byte are accepted

1 participant