feat: Node::free_subtree — immediate discard with wrapper neutralization - #210
Merged
Conversation
unlink_node alone never frees a doc-owned node, and set_rust_owned defers the free to the last wrapper drop — which a stray clone in a long-lived collection postpones past the owning document's xmlFreeDoc, turning the eventual xmlFreeNode into a use-after-free (observed: SIGSEGV in xmlDictOwns when a consumer's bookkeeping Vec dropped after the Document). free_subtree frees the C subtree NOW: it unlinks, walks the subtree iteratively (elements, texts, attributes), nulls the shared node_ptr of every registered wrapper (clones anywhere become inert no-ops) and clears the bookkeeping entries, then calls xmlFreeNode once. Documents and document fragments are refused. Driver: latexml-oxide's math parser discards thousands of replaced subtrees per document; without a true free they leak ~1.4 MB per formula. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.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.
What. A discard operation that actually frees:
Node::free_subtreedetaches the node, walks its C subtree iteratively (elements, texts, attributes), nulls the sharednode_ptrof every registered wrapper (stray clones anywhere become inert no-ops) and clears the bookkeeping entries, then callsxmlFreeNodeonce. Documents/fragments are refused.Why.
unlink_nodealone never frees a doc-owned node, andset_rust_owneddefers the free to the last wrapper drop — which a clone parked in a long-lived collection postpones past the owning document'sxmlFreeDoc, turning the eventual free into a use-after-free (observed: SIGSEGV inxmlDictOwns). Driver: latexml-oxide's math parser discards thousands of replaced subtrees per document (~1.4 MB/formula leaked; 31.2M leaked allocations on a 2 MB slice, ~99% eliminated by this).Validation. New
tests/free_subtree_tests.rs(5 tests: held-wrapper neutralization incl. post-document-drop, detached and never-linked trees, double-free-via-clone no-op, document refusal); full fork suite green; release prep 0.3.17 included.🤖 Generated with Claude Code