Skip to content

Fix GH-22570: stack overflow serializing a deeply nested Dom\XMLDocument#22576

Closed
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-22570-dom-xml-serialize-stack
Closed

Fix GH-22570: stack overflow serializing a deeply nested Dom\XMLDocument#22576
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-22570-dom-xml-serialize-stack

Conversation

@iliaal

@iliaal iliaal commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

A deeply nested Dom\XMLDocument overflows the C stack when serialized, since the XML serializer recurses per element child. Adds a stack-limit check at the dispatcher (throwing an Error on overflow, like bd724bd for var serialization) and gates the saveXml warning and innerHTML exception with !EG(exception) so the thrown Error isn't masked or replaced.

Fixes #22570

…cument

The new-DOM XML serializer recurses through dom_xml_serialization_algorithm()
for every element child, so a document nested deeply enough overflows the C
stack and crashes during saveXml() or innerHTML. Add a stack-limit check at
the dispatcher, throwing an Error on overflow, mirroring bd724bd. Gate the
"Could not save document" warning and the innerHTML not-well-formed exception
with !EG(exception) so the thrown Error propagates cleanly instead of being
accompanied by a warning or replaced with the wrong exception type.

Fixes phpGH-22570
@iliaal iliaal closed this in dd57694 Jul 3, 2026
@iluuu1994

Copy link
Copy Markdown
Member

The test seems to fail frequently in nightly. Can you please check?

https://github.com/php/php-src/actions/runs/28725922777/job/85183280824

@iliaal

iliaal commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

That's a timeout, not a wrong result. The test builds a 100k-node tree, and on the macOS ZTS runner that clears the 120s limit; the serialization it actually checks bails at the stack guard instantly. I couldn't reproduce the slowdown locally (runs in ~0.02s here, even with the OS stack constrained to 512K to mimic the ZTS thread), so the cost looks like a per-node constant factor specific to that runner. At zend.max_allowed_stack_size=512K the overflow already triggers well under 10k nested nodes, so I've cut the depth from 100k to 10k, which keeps the stack-guard coverage with room to spare. #22626.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stack overflow when serializing a deeply nested Dom\XMLDocument

3 participants