Skip to content

[Bug]: Deep binary expressions overflow Spoon visitors #6804

Description

@martinfrancois

Describe the bug

OpenJDK's valid DeepStringConcat.java contains about 32,000 binary operators in one expression. AstParentConsistencyChecker and ProcessingVisitor recursively visit one operator per JVM stack frame.

The first overflow occurs while AstParentConsistencyChecker checks parent links. If that check is bypassed, ProcessingVisitor reaches the same limit while running processors.

A compatible fix may use small explicit stacks for consecutive binary operands in these two concrete visitors. Regardless of implementation, it must preserve Spoon's normal child order, callbacks before and after each child, and subclass override behavior. Malformed-parent diagnostics must not recursively pretty-print the same deep tree.

Source code you are trying to analyze/transform

DeepStringConcat.java from openjdk/jdk@be40b6bcdab37368ea3c769e575b36e290d0c6a1.

The file is a valid compiler stress test with a very deep string-concatenation expression.

Source code for your Spoon processing

Building the model exposes the parent-consistency failure:

Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("DeepStringConcat.java");
launcher.buildModel();

To expose the separate processor traversal failure without first running the parent checker, disable that check and run a processor:

Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().disableConsistencyChecks();
launcher.addInputResource("DeepStringConcat.java");
launcher.addProcessor(new AbstractProcessor<CtBinaryOperator<?>>() {
    @Override
    public void process(CtBinaryOperator<?> operator) {
    }
});
launcher.run();

Actual output

java.lang.StackOverflowError
    at spoon.reflect.visitor.CtScanner.visitCtBinaryOperator(...)
    at spoon.support.reflect.code.CtBinaryOperatorImpl.accept(...)
    at spoon.reflect.visitor.AstParentConsistencyChecker.scan(...)
    ... repeats ...

With consistency checks disabled, the processor run overflows in ProcessingVisitor instead.

Expected output

Spoon validates and processes the legal deep expression with the normal JVM stack size.

Spoon Version

Reproduced on Spoon master at 06b187819380700384762416d15db2cc4f112175.

JVM Version

OpenJDK 25.0.3

What operating system are you using?

Linux x86-64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions