Fix StackOverflowError in recursive AST conversion#186
Open
elevenfive wants to merge 1 commit intohalilozercan:mainfrom
Open
Fix StackOverflowError in recursive AST conversion#186elevenfive wants to merge 1 commit intohalilozercan:mainfrom
elevenfive wants to merge 1 commit intohalilozercan:mainfrom
Conversation
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>
|
cc @halilozercan can we approve the workflow to test? |
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.
Summary
The
convert()function inAstNodeConvert.ktconverts a CommonMarkNodetree into anAstNodetree using recursion in two directions — the sibling chain (node.next) and the child chain (node.firstChild). Both recurse without bound, causingStackOverflowErrorcrashes in production on Android's ~1MB default thread stack when documents contain many sequential blocks or deeply nested structures.Crashlytics stack trace:
Changes:
convert()with an iterative implementation: siblings are traversed in awhileloop, and child processing is pushed onto an explicitArrayDequestackwhenblock into a privateconvertNodeType()helper (no logic changes)AstNodetree is identical (allAstNodeLinks—parent,firstChild,lastChild,next,previous— are wired up the same way)Tests added:
convert()on a thread with a 256KB stack — first proves that equivalent-depth recursion overflows, then proves the iterativeconvert()handles itTest plan
./gradlew :richtext-commonmark:allTestspasses (6 tests, 0 failures)StackOverflowErroron 2,000 siblings; assertion failure on the constrained-stack test)image without title) continues to pass unchanged🤖 Generated with Claude Code