Skip to content

Avoid stack exhaustion in REXML::Formatters::Default#write_element - #358

Merged
kou merged 1 commit into
ruby:masterfrom
naitoh:fix-deep-write
Aug 16, 2026
Merged

Avoid stack exhaustion in REXML::Formatters::Default#write_element#358
kou merged 1 commit into
ruby:masterfrom
naitoh:fix-deep-write

Conversation

@naitoh

@naitoh naitoh commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #354

REXML::Formatters::Default#write_element recursed through REXML::Formatters::Default#write for every child element, so the nesting depth of the document became the depth of the machine stack. Writing a document nested about 2000 levels deep raised SystemStackError, after having already written the partial output to the caller's output object.

Walk the descendants with an explicit stack instead. A String on the stack is an end tag waiting to be written, so the pending end tags cost one entry per open ancestor.

Fixes ruby#354

`REXML::Formatters::Default#write_element` recursed through `REXML::Formatters::Default#write` for every child element, so the nesting depth of the document became the depth of the machine stack.
Writing a document nested about 2000 levels deep raised SystemStackError, after having already written the partial output to the caller's output object.

Walk the descendants with an explicit stack instead.
A String on the stack is an end tag waiting to be written, so the pending end tags cost one entry per open ancestor.
@naitoh
naitoh requested a review from kou August 16, 2026 09:45
@kou
kou merged commit 9694327 into ruby:master Aug 16, 2026
71 checks passed
@naitoh
naitoh deleted the fix-deep-write branch August 16, 2026 10:13
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.

Ruby bundled REXML Document#write stack exhaustion on deeply nested XML

2 participants