Skip to content

Harden save output buffer against slot leak and split multibyte chars - #165

Merged
jameslan merged 3 commits into
jameslan:masterfrom
shunkica:fix/save-output-hardening
Aug 27, 2026
Merged

jameslan merged 3 commits into
jameslan:masterfrom
shunkica:fix/save-output-hardening

Conversation

@shunkica

@shunkica shunkica commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #164

Fix

Two hardening fixes to the save output-buffer path.

Leaked handler slot and silent failure on NULL save context

xmlSaveToIO/xmlOutputBufferCreateIO allocate an outputHandlerStorage slot that is meant to be freed by the outputClose callback. When libxml2 returns NULL (e.g. an unsupported encoding), that callback never fires, so the slot leaked permanently. On the save() path this also silently produced empty output.

  • Centralized the allocate/free-on-NULL lifecycle in one withOutputHandler helper, so the "free the slot if libxml2 never took ownership" invariant lives in a single place instead of being duplicated across both callers.
  • xmlSaveToIO now throws XmlError naming the unsupported encoding rather than returning NULL:
XmlError: Unsupported save encoding "no-such-encoding"

Placing the throw in xmlSaveToIO co-locates it with the free that reacts to the same NULL, and removes the check from both XmlDocument.save and XmlElement.save.

Multibyte characters corrupted when split across writes

XmlStringOutputBufferHandler (src/utils.mts) decoded each write() chunk as a standalone buffer. A multibyte character straddling a chunk boundary decoded to U+FFFD replacement characters.

  • write() now uses TextDecoder.decode(buf, { stream: true }) and close() flushes the decoder with a final decode().

@shunkica
shunkica force-pushed the fix/save-output-hardening branch 2 times, most recently from 9ad86c7 to 6a52e57 Compare August 4, 2026 09:38
…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
shunkica force-pushed the fix/save-output-hardening branch from 6a52e57 to 603c0ea Compare August 4, 2026 09:44
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).
@github-actions

Copy link
Copy Markdown

Coverage after merging fix/save-output-hardening into master will be

100.00%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
src
   c14n.mts100%100%100%100%
   diag.mts99.51%100%90.91%100%
   disposable.mts100%100%100%100%
   disposeShim.mts100%100%100%100%
   document.mts100%100%100%100%
   dtd.mts100%100%100%100%
   index.mts100%100%100%100%
   libxml2.mts99.90%99.20%100%100%676
   metadataShim.mts100%100%100%100%
   nodejs.mts100%100%100%100%
   nodes.mts99.92%99.22%100%100%76
   utils.mts100%100%100%100%
   validates.mts100%100%100%100%
   xpath.mts100%100%100%100%

@jameslan
jameslan merged commit 79e6bdd into jameslan:master Aug 27, 2026
14 checks passed
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>
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.

save() silently does nothing on an unsupported encoding (and leaks the output handler)

2 participants