Repository navigation
Conversation
Non-UTF8-flagged Perl SVs store characters as Latin-1 bytes internally. When the document encoding is UTF-8, PmmFastEncodeString received XML_CHAR_ENCODING_UTF8 as the source charset, causing it to treat the Latin-1 bytes as already-valid UTF-8 and copy them unchanged. This meant upgraded and downgraded SVs with the same logical content produced different (and invalid) output — the classic Perl "Unicode Bug". Fix: when the document encoding is UTF-8 and the SV lacks the UTF-8 flag, use XML_CHAR_ENCODING_8859_1 as the source encoding so PmmFastEncodeString correctly converts Latin-1 bytes to UTF-8. For non-UTF-8 document encodings (e.g. UTF-16), the old behavior is preserved since the caller may legitimately pass encoded bytes. Also handle the fallback path (no document or no document encoding) where Sv2C(scalar, NULL) would pass Latin-1 bytes through unchanged.
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.
What
Fix the Perl Unicode Bug in
nodeSv2Cso thatappendText,setAttribute, and allother node string setters behave identically regardless of whether a Perl string is
internally upgraded (UTF-8 flagged) or downgraded (Latin-1 bytes).
Why
Reported by ikegami in RT #115190
(2016), migrated to GH #229. Non-UTF8-flagged Perl SVs store characters 0x80–0xFF as
single Latin-1 bytes. When the document encoding is UTF-8,
PmmFastEncodeStringreceivedXML_CHAR_ENCODING_UTF8as the source charset, causing a no-opxmlStrdup— the rawLatin-1 bytes were passed to libxml2 as if they were valid UTF-8. This produced different
(and invalid) XML depending on Perl's internal string representation, which should be
invisible to XS code.
How
!DO_UTF8(scalar)and the document encoding is UTF-8, passXML_CHAR_ENCODING_8859_1as the source encoding toPmmFastEncodeStringso theLatin-1→UTF-8 conversion actually runs.
may legitimately pass bytes in the document's encoding.
Sv2C(scalar, NULL)would pass Latin-1 bytes through unchanged.
Testing
t/49unicode_bug_rt115190.tbased on ikegami's reproducer: round-tripsupgraded vs downgraded Latin-1 strings through text nodes, attributes, and a document
without explicit encoding.
t/03doc.tUTF-16 getAttribute tests still pass (the fix is scoped toUTF-8 documents only).
Closes #229
Quality Report
Changes: 2 files changed, 128 insertions(+), 5 deletions(-)
Code scan: clean
Tests: passed (OK)
Branch hygiene: clean
Generated by Kōan