Skip to content

Catch out-of-range numeric character references (#591) - #592

Closed
ChrisJr404 wants to merge 1 commit into
kurtmckee:mainfrom
ChrisJr404:fix-591-charref-out-of-range
Closed

Catch out-of-range numeric character references (#591)#592
ChrisJr404 wants to merge 1 commit into
kurtmckee:mainfrom
ChrisJr404:fix-591-charref-out-of-range

Conversation

@ChrisJr404

Copy link
Copy Markdown

This fixes #591. The loose parser resolved numeric character references with chr(c).encode("utf-8"), so a reference that overflows the Unicode range like �, sits above the maximum code point like �, or is a lone surrogate like � would raise OverflowError, ValueError, or UnicodeEncodeError and take down the whole parse.

I wrapped just the conversion in a try/except and fall back to keeping the reference as literal text when it can't be turned into a character. That mirrors what _BaseHTMLProcessor.handle_charref in html.py already does for the same input, so the two paths now agree instead of one of them crashing.

Added a small test module covering the three exception types plus a valid reference to make sure normal ones still resolve, and a changelog fragment. The three examples from the issue are used directly as test cases.

The loose parser resolved every numeric character reference with
chr().encode("utf-8"), so a reference whose value overflowed the Unicode
range (�), landed above the maximum code point (�),
or was a lone surrogate (�) raised OverflowError, ValueError, or
UnicodeEncodeError and aborted the whole parse.

Wrap the conversion so those references fall back to their literal text,
which is how the HTML processor already handles them. Fixes kurtmckee#591.
@kurtmckee

Copy link
Copy Markdown
Owner

I suspect this was generated by AI. I'm closing this PR.

@kurtmckee kurtmckee closed this Aug 22, 2026
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.

Catch out-of-range numeric character reference exceptions

2 participants