Repository navigation
Harden save output buffer against slot leak and split multibyte chars - #165
Merged
Merged
Conversation
shunkica
force-pushed
the
fix/save-output-hardening
branch
2 times, most recently
from
August 4, 2026 09:38
9ad86c7 to
6a52e57
Compare
…chars Closes jameslan#164. xmlSaveToIO/xmlOutputBufferCreateIO leaked an outputHandlerStorage slot whenever libxml2 returned NULL (e.g. an unsupported encoding), because the close callback that frees the slot never fired. XmlDocument.save/ XmlElement.save also silently produced empty output in that case. The allocate/free-on-null lifecycle is now centralized in a withOutputHandler helper, and xmlSaveToIO throws XmlError naming the unsupported encoding instead of returning NULL. XmlStringOutputBufferHandler decoded each write() as a standalone buffer, corrupting multibyte characters split across chunks. It now decodes with { stream: true } and flushes in close().
shunkica
force-pushed
the
fix/save-output-hardening
branch
from
August 4, 2026 09:44
6a52e57 to
603c0ea
Compare
libxml2 is the only writer and never passes partial UTF-8, so the streaming TextDecoder was dead code. Reverts that part of 603c0ea (jameslan#164).
jameslan
approved these changes
Aug 27, 2026
|
Coverage after merging fix/save-output-hardening into master will be
Coverage Report
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
jameslan
added a commit
that referenced
this pull request
Sep 5, 2026
…#165) * fix: harden XML save output buffer against leaks and split multibyte chars Closes #164. xmlSaveToIO/xmlOutputBufferCreateIO leaked an outputHandlerStorage slot whenever libxml2 returned NULL (e.g. an unsupported encoding), because the close callback that frees the slot never fired. XmlDocument.save/ XmlElement.save also silently produced empty output in that case. The allocate/free-on-null lifecycle is now centralized in a withOutputHandler helper, and xmlSaveToIO throws XmlError naming the unsupported encoding instead of returning NULL. XmlStringOutputBufferHandler decoded each write() as a standalone buffer, corrupting multibyte characters split across chunks. It now decodes with { stream: true } and flushes in close(). * revert: streaming decode in XmlStringOutputBufferHandler libxml2 is the only writer and never passes partial UTF-8, so the streaming TextDecoder was dead code. Reverts that part of 603c0ea (#164). --------- Co-authored-by: James Lan <jameslan@gmail.com>
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.
Closes #164
Fix
Two hardening fixes to the save output-buffer path.
Leaked handler slot and silent failure on NULL save context
xmlSaveToIO/xmlOutputBufferCreateIOallocate anoutputHandlerStorageslot that is meant to be freed by theoutputClosecallback. When libxml2 returns NULL (e.g. an unsupported encoding), that callback never fires, so the slot leaked permanently. On thesave()path this also silently produced empty output.withOutputHandlerhelper, so the "free the slot if libxml2 never took ownership" invariant lives in a single place instead of being duplicated across both callers.xmlSaveToIOnow throwsXmlErrornaming the unsupported encoding rather than returning NULL:Placing the throw in
xmlSaveToIOco-locates it with the free that reacts to the same NULL, and removes the check from bothXmlDocument.saveandXmlElement.save.Multibyte characters corrupted when split across writes
XmlStringOutputBufferHandler(src/utils.mts) decoded eachwrite()chunk as a standalone buffer. A multibyte character straddling a chunk boundary decoded toU+FFFDreplacement characters.write()now usesTextDecoder.decode(buf, { stream: true })andclose()flushes the decoder with a finaldecode().