Skip to content

Fix StackOverflowError in recursive AST conversion#186

Open
elevenfive wants to merge 1 commit intohalilozercan:mainfrom
elevenfive:fix/iterative-ast-conversion
Open

Fix StackOverflowError in recursive AST conversion#186
elevenfive wants to merge 1 commit intohalilozercan:mainfrom
elevenfive:fix/iterative-ast-conversion

Conversation

@elevenfive
Copy link

Summary

The convert() function in AstNodeConvert.kt converts a CommonMark Node tree into an AstNode tree using recursion in two directions — the sibling chain (node.next) and the child chain (node.firstChild). Both recurse without bound, causing StackOverflowError crashes in production on Android's ~1MB default thread stack when documents contain many sequential blocks or deeply nested structures.

Crashlytics stack trace:

java.lang.StackOverflowError: stack size 1037KB
  at com.halilibo.richtext.markdown.node.AstNode.<init> (AstNode.kt:4)
  at com.halilibo.richtext.commonmark.AstNodeConvertKt.convert (AstNodeConvert.kt:170)
  at com.halilibo.richtext.commonmark.AstNodeConvertKt.convert (AstNodeConvert.kt:174)
  at com.halilibo.richtext.commonmark.AstNodeConvertKt.convert (AstNodeConvert.kt:175)
  ... (hundreds of frames)

Changes:

  • Replaced the recursive convert() with an iterative implementation: siblings are traversed in a while loop, and child processing is pushed onto an explicit ArrayDeque stack
  • Extracted the node-type mapping when block into a private convertNodeType() helper (no logic changes)
  • The function signature and external behavior are unchanged — the produced AstNode tree is identical (all AstNodeLinksparent, firstChild, lastChild, next, previous — are wired up the same way)

Tests added:

  • Tree link correctness for documents with siblings (parent/previous/next/lastChild)
  • Tree link correctness for nested structures (blockquote → paragraph → text)
  • 2,000 sibling paragraphs complete without overflow
  • 500 levels of nested blockquotes complete without overflow
  • Constrained-stack proof: builds a 5,000-sibling CommonMark tree and runs convert() on a thread with a 256KB stack — first proves that equivalent-depth recursion overflows, then proves the iterative convert() handles it

Test plan

  • ./gradlew :richtext-commonmark:allTests passes (6 tests, 0 failures)
  • Verified the old recursive implementation fails 2 of the new tests (StackOverflowError on 2,000 siblings; assertion failure on the constrained-stack test)
  • Existing test (image without title) continues to pass unchanged

🤖 Generated with Claude Code

Replace the recursive convert() function with an iterative implementation
using an explicit stack, preventing StackOverflowError on deeply nested
or long markdown documents (e.g. Android's ~1MB default thread stack).

Siblings are now traversed in a loop and children are pushed onto an
explicit ArrayDeque, producing the identical AstNode tree structure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mzavislak-inflection
Copy link

cc @halilozercan can we approve the workflow to test?

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.

2 participants