Skip to content

Fix stack overflow in XMLElement::Accept for deeply nested trees - #1092

Open
afonsojanu wants to merge 1 commit into
leethomason:masterfrom
afonsojanu:fix/accept-recursion-stack-overflow
Open

Fix stack overflow in XMLElement::Accept for deeply nested trees#1092
afonsojanu wants to merge 1 commit into
leethomason:masterfrom
afonsojanu:fix/accept-recursion-stack-overflow

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes the Accept() half of #839, following up on #1091 which fixed the DeepClone/DeepCopy and node-teardown side.

XMLElement::Accept walks element children by recursing through node->Accept(), one call frame per level. A tree built up at runtime through InsertEndChild/InsertNewChildElement isn't bounded by XMLDocument::DepthTracker the way a parsed document is, so a long enough chain of single-child elements pushes this past the stack limit.

Text/comment/declaration/unknown children are always leaves and can't nest further, so those are still visited directly through their own Accept(). Only descending into an element child now goes through an explicit, heap-backed stack standing in for the call stack a recursive version would use, keeping the same VisitEnter/VisitExit ordering and early-exit-on-false behavior as the original.

Added a test that builds a 60000-level-deep tree and runs Accept() over it with an XMLPrinter, which reliably overflowed the stack before this change. Full suite: 530 passing both before and after (the one new test taking the place of the crash), same result under ASan+UBSan. The chain is torn back down one leaf at a time in the test itself rather than left to the default destructor, since that recursion is the separate, not-yet-merged half of #1091.

XMLElement::Accept walks element children by recursing through
node->Accept(), one call frame per tree level. A tree built up at
runtime through InsertEndChild/InsertNewChildElement isn't bounded by
XMLDocument::DepthTracker the way a parsed document is, so a long
enough chain of single-child elements can push this past the stack
limit, same shape of bug as the one leethomason#1091 already fixed for
DeepClone/DeepCopy and node teardown (see leethomason#839, where this was called
out as a separate follow-up).

Text/comment/declaration/unknown children are always leaves and can't
nest further, so they're still visited directly through their own
Accept(). Only descending into an element child now goes through an
explicit, heap-backed stack that stands in for the call stack a
recursive version would use, while keeping the same VisitEnter/
VisitExit ordering and early-exit-on-false behavior as before.

Added a test building a 60000-level-deep tree and running Accept()
over it with an XMLPrinter, which reliably overflowed the stack before
this change. Confirmed against the full suite: 530 passing both before
and after (the one new test replacing the crash), same result under
ASan+UBSan.
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.

1 participant