Fix XML.unescape rejecting valid whitespace numeric character references#1070
Open
dong0713 wants to merge 1 commit into
Open
Fix XML.unescape rejecting valid whitespace numeric character references#1070dong0713 wants to merge 1 commit into
dong0713 wants to merge 1 commit into
Conversation
XML.mustEscape() is shared by both XML.escape() (serialization) and
XMLTokener.unescapeEntity() (deserialization). While the method's Javadoc
and comment quote the W3C XML 1.0 valid-character range
(#x9 | #xA | #xD | [#x20-#xD7FF] | ...), the implementation only checked
[#x20-#xD7FF] in its range clause, omitting #x9/#xA/#xD.
Although the ISO-control clause excluded those three codepoints, the
negated range clause still marked them as 'must escape', so unescapeEntity()
threw JSONException for the XML-allowed control characters TAB, LF and CR.
This broke XML.unescape("&stleary#10;") and XML.toJSONObject("<a>&stleary#10;</a>"),
both of which worked in v20251224 and regressed after stleary#1045 (v20260522).
Align the range clause with the W3C spec by explicitly allowing #x9, #xA
and #xD, matching the comment that was already documented.
Fixes stleary#1059
|
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
Fixes #1059 —
XML.unescape(" ")andXML.toJSONObject("<a> </a>")throwJSONException: Invalid numeric character reference: in versions after 20251224 (regression introduced by #1045).Root Cause
XML.mustEscape(int cp)is shared by both the serialization path (XML.escape) and the deserialization path (XMLTokener.unescapeEntity). The method's Javadoc and comment correctly quote the W3C XML 1.0 valid-character range:However, the implementation only checked
[#x20-#xD7FF]onwards in its range clause, omitting#x9/#xA/#xD(TAB, LF, CR). Although the ISO-control clause explicitly excluded those three codepoints, the negated range clause (!(...)) still evaluated totruefor them, somustEscape(0xA)returnedtrueandunescapeEntitythrewJSONException.Fix
Align the range clause with the W3C spec by explicitly allowing
cp == 0x9,cp == 0xA, andcp == 0xD, matching the comment that was already documented.Before:
|| !( (cp >= 0x20 && cp <= 0xD7FF) // missing #x9, #xA, #xD || (cp >= 0xE000 && cp <= 0xFFFD) || (cp >= 0x10000 && cp <= 0x10FFFF) )After:
|| !( cp == 0x9 || cp == 0xA || cp == 0xD || (cp >= 0x20 && cp <= 0xD7FF) || (cp >= 0xE000 && cp <= 0xFFFD) || (cp >= 0x10000 && cp <= 0x10FFFF) )Behaviour Change
XML.unescape(" ")JSONException"\n"XML.unescape("	")JSONException"\t"XML.unescape(" ")JSONException"\r"XML.unescape("
")JSONException"\n"XML.toJSONObject("<a> </a>")JSONException{"a":""}XML.unescape(" ")" "(unchanged)" "Testing
testValidWhitespaceNumericEntityUnescape— verifies decimal and hex references for TAB/LF/CR viaXML.unescape()testValidWhitespaceNumericEntityToJSONObject— verifiesXML.toJSONObject()accepts	, , without throwing (regression test for org.json.XML.unescape(" "); throws org.json.JSONException: Invalid numeric character reference: #1059)Fixes #1059