Skip to content

Fix NULL dereference in XMLNode::InsertAfterChild for adjacent re-insertion - #1103

Open
zhouyunding wants to merge 1 commit into
leethomason:masterfrom
zhouyunding:work/issue-1101
Open

zhouyunding wants to merge 1 commit into
leethomason:masterfrom
zhouyunding:work/issue-1101

Conversation

@zhouyunding

@zhouyunding zhouyunding commented Oct 20, 2026 •

Copy link
Copy Markdown

Fixes #1101

Disclosure: this PR was prepared with AI-assisted tooling; the change and tests were reviewed and validated by the human contributor.

Root cause

XMLNode::InsertAfterChild(a, b) crashes when b is already a's immediate next sibling and is the last child, for example with <r><a/><b/></r>. The insertion preamble unlinks b (InsertChildPreamble → Unlink), which sets a->_next to b->_next (i.e. 0); the following code then writes through afterThis->_next->_prev, dereferencing the just-cleared pointer — a null-pointer write (SEGV, exit 139).

Fix

Return addThis without unlinking it when it is already in the requested position (afterThis->_next == addThis). The guard follows the existing document, parent, and self-insertion checks, so it cannot bypass any of them; returning addThis matches the header contract ("Returns the addThis argument or 0 if..."). Add regressions for adjacent last and middle nodes, covering the return pointer, first/last child, parent, bidirectional sibling links, and repeated insertion (idempotence, no duplicates or self-loops).

Behavior change: re-inserting a node at its current position is now an idempotent no-op that returns addThis, instead of crashing when the node is the last child (or needlessly unlinking/relinking otherwise). All existing move, self-insertion, and cross-document rejection semantics are unchanged.

Validation on Windows with GCC 16.2.0, CMake/Ninja, static builds (reproduced again in independent fresh builds)

  • Original-source probe and the new regression with the old implementation both crash with exit 139 (SEGV); the fixed probe returns successfully with tree links intact (exit 0).
  • Debug and Release: full CTest passed (1/1); xmltest reports Pass 554, Fail 0 in each configuration. The unmodified baseline reports Pass 528, Fail 0 — the +26 new assertions all pass and the existing 528 assertions show no regression.
  • git -c core.whitespace=cr-at-eol diff --check passed; the repository's existing CRLF line endings are preserved.
  • Not run locally: ASan/UBSan, MSVC, and shared-library builds.

…ertion

InsertAfterChild unlinked addThis before checking whether it was already
the immediate next sibling of afterThis. Unlinking addThis cleared
afterThis->_next, so the subsequent write to afterThis->_next->_prev
dereferenced a null pointer. Return addThis early when it is already in
place (issue leethomason#1101).
@zhouyunding

Copy link
Copy Markdown
Author

Maintainers, when you have a chance, could you please review this fix? The PR description includes local Debug/Release CTest and xmltest results, but no remote checks are currently reported. Please let me know if any additional CI jobs or tests would be helpful. Thank you.

This branch has not been deployed

No deployments
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.

XMLNode::InsertAfterChild dereferences NULL when addThis is already afterThis's next sibling

1 participant