Skip to content

fix: addSibling double-free when text merging via last sibling#263

Draft
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/fix-issue-246
Draft

fix: addSibling double-free when text merging via last sibling#263
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/fix-issue-246

Conversation

@toddr-bot

@toddr-bot toddr-bot commented May 22, 2026

Copy link
Copy Markdown
Contributor

Summary

addSibling had incomplete text-merge detection: it only checked whether self was a text node, but xmlAddSibling actually navigates to self->parent->last before merging. When self was not text but the last sibling was, libxml2 merged and freed nNode without the proxy being disconnected — causing a use-after-free / double-free.

Replaced the two-branch predict-and-copy approach with the same ret != nNode detection pattern used by addChild: save the proxy before calling xmlAddSibling, then NULL it out if the node was consumed by a merge. This handles all merge cases regardless of which sibling triggers the merge.

Fixes #246

Changes

  • Unified addSibling merge handling to follow addChild's proven pattern
  • Added regression test reproducing the exact scenario from the bug report (two addSibling calls with text nodes on a non-text element)

Test plan

  • New test in t/04node.t reproduces the double-free scenario from the issue
  • Existing RT #94149 text-merge test continues to pass
  • Full test suite passes (2655 tests, 0 failures)

Generated by Kōan /fix


Quality Report

Changes: 2 files changed, 41 insertions(+), 27 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

xmlAddSibling navigates to parent->last before checking for text
merges. The old code only checked self->type, so when self was not
a text node but parent->last was, libxml2 merged and freed nNode
without the proxy being disconnected — use-after-free / double-free.

Replace the two-branch predict-and-copy approach with the same
ret != nNode detection pattern used by addChild: save the proxy
before calling xmlAddSibling, then NULL it out if the node was
consumed by a merge.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.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.

RT #125129: Another double free set of errors

1 participant