Fix stack overflow in XMLElement::Accept for deeply nested trees - #1092
Open
afonsojanu wants to merge 1 commit into
Open
Fix stack overflow in XMLElement::Accept for deeply nested trees#1092afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
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.
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.
Fixes the Accept() half of #839, following up on #1091 which fixed the DeepClone/DeepCopy and node-teardown side.
XMLElement::Acceptwalks element children by recursing throughnode->Accept(), one call frame per level. A tree built up at runtime throughInsertEndChild/InsertNewChildElementisn't bounded byXMLDocument::DepthTrackerthe 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 sameVisitEnter/VisitExitordering 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 anXMLPrinter, 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.