Repository navigation
Conversation
A numeric character reference to a code point in the surrogate range, such as `�`, produced a lone surrogate in the text. The spec says that invalid Unicode code points are replaced by the REPLACEMENT CHARACTER, and a surrogate is not a valid Unicode code point; cmark and commonmark.js both replace the whole range. The resulting string is not valid UTF-16 and cannot be encoded, so it turned into "?" when the rendered HTML was written as UTF-8. Html5Entities.entityToString only special-cased 0 and relied on Character.toChars throwing for values above U+10FFFF, but Character.toChars accepts the surrogate range.
This branch has not been deployed
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.
Problem
A numeric character reference to a code point in the surrogate range is kept as a lone surrogate:
The spec, Entity and numeric character references: "Invalid Unicode code points will be replaced by the REPLACEMENT CHARACTER (
U+FFFD)". A code point in the surrogate range (U+D800–U+DFFF) is not a valid Unicode code point. Thespec.txtused by the tests only exercises�, so the spec tests do not cover this.Both reference implementations replace the range: cmark in
houdini_unescape_ent(src/houdini_html_u.c:codepoint == 0 || (codepoint >= 0xD800 && codepoint < 0xE000) || codepoint >= 0x110000→0xFFFD), and commonmark.js 0.31.2 renders<p>+ U+FFFD +</p>for the same input.Because a lone surrogate is not valid UTF-16, the corruption is visible downstream as well: writing the rendered HTML as UTF-8 replaces it with
?.Fix
Html5Entities.entityToStringonly special-cased0and relied onCharacter.toCharsthrowing for values above U+10FFFF, butCharacter.toChars(0xD800)succeeds. The surrogate range is now rejected in the same way as0, usingCharacter.MIN_SURROGATE/MAX_SURROGATE, so valid supplementary code points are not affected.entityToStringis also used when unescaping link destinations and link reference definitions, so those are covered by the same change.Tests
Added
SpecialInputTest.surrogateCharacterReferenceShouldBeReplaced, next to the existingnullCharacterEntityShouldBeReplaced:�,�,�andbefore�after→ U+FFFD😀(U+1F600) andunchangedVerification:
Tests run: 1, Failures: 1SpecialInputTest→Tests run: 33, Failures: 0mvn -B packageon all modules with JDK 17: BUILD SUCCESS, including the spec tests (1959 tests, 0 failures)mvn spotless:check: clean