Paul King created GROOVY-12709:
----------------------------------
Summary: Type checkers overflow the stack on fields whose
initializers refer to each other
Key: GROOVY-12709
URL: https://issues.apache.org/jira/browse/GROOVY-12709
Project: Groovy
Issue Type: Bug
Affects Versions: 6.0.0, 5.1.3
Reporter: Paul King
h2. Problem
With {{groovy.typecheckers.RegexChecker}} or
{{groovy.typecheckers.FormatStringChecker}} enabled, compiling code that uses a
field whose initializer refers, directly or through other fields, back to
itself makes the compiler throw {{java.lang.StackOverflowError}}. The error
isn't turned into a compilation error, so the compile is aborted.
Reproducer, run with {{groovy --configscript config.groovy cycle.groovy}}:
{code:groovy|title=config.groovy}
import groovy.transform.TypeChecked
withConfig(configuration) {
ast(TypeChecked, extensions: 'groovy.typecheckers.RegexChecker')
}
{code}
{code:groovy|title=cycle.groovy}
import java.util.regex.Pattern
class Cycle {
static final String R = S
static final String S = R
static Pattern compile() { Pattern.compile(R) }
}
println 'compiled'
{code}
Actual:
{noformat}
Caught: java.lang.StackOverflowError
at
org.apache.groovy.typecheckers.CheckingVisitor.findTargetVariable(CheckingVisitor.groovy)
at
org.apache.groovy.typecheckers.CheckingVisitor.findConstExp(CheckingVisitor.groovy)
at
org.apache.groovy.typecheckers.CheckingVisitor.findConstExp(CheckingVisitor.groovy)
...
{noformat}
Expected: the script compiles and prints {{compiled}}, as it does without the
extension. The fields have no constant value to find, so there is nothing to
check (both fields are {{null}} at runtime).
The same happens with {{FormatStringChecker}}, for example with
{{String.format(F, 1)}} where {{static final String F = G}} and {{static final
String G = F}}.
||Version||RegexChecker||FormatStringChecker||
|4.0.32|StackOverflowError|(checker not available)|
|5.1.3|StackOverflowError|StackOverflowError|
|6.0.0|StackOverflowError|StackOverflowError|
h2. Cause
{{CheckingVisitor.findConstExp}} finds the constant value of an expression, and
for a variable that resolves to a field with an initializer it calls itself on
that initializer. Nothing records which fields it has already followed, so a
chain of initializers that leads back to an earlier field recurses until the
stack overflows.
{{findConstExp}} is shared by RegexChecker and FormatStringChecker; the other
checkers in groovy-typecheckers don't call it.
h2. Proposed fix
Track the variables already followed during one lookup, and return {{null}} (no
constant value) when a variable is reached a second time. This matches how
{{findConstExp}} already treats any other value it can't determine.
h2. Tests
* The reproducer above, for both RegexChecker and FormatStringChecker:
compiles, and runs.
* A longer cycle ({{A = B}}, {{B = C}}, {{C = A}}).
* A field initialized from another field that does have a constant value
({{static final String R = S}}, {{static final String S = /(a)/}}) is still
followed and checked, for example a bad group number is still reported.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)