[ 
https://issues.apache.org/jira/browse/GROOVY-12709?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-12709:
-------------------------------
    Description: 
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 String R = S
    static String S = R
    static Pattern compile() { Pattern.compile(R) }
}
println 'compiled ' + Cycle.R
{code}

Actual:
{noformat}
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 null}}, as it does without 
the extension. The fields have no constant value to find, so there is nothing 
to check.

The same happens with {{FormatStringChecker}}, for example {{String.format(F, 
1)}} with {{static String F = G}} and {{static String G = F}}, and with 
{{final}} instance fields ({{final String a = b}}, {{final String b = a}}).

||Version||RegexChecker||FormatStringChecker||
|4.0.32|StackOverflowError|(checker not available)|
|5.1.3|StackOverflowError|StackOverflowError|
|6.0.0|StackOverflowError|StackOverflowError|

The same cycle of {{static final}} fields overflows the compiler even without 
these checkers; that is GROOVY-12710.

h2. Cause

{{CheckingVisitor.findConstExp}} finds the constant value of an expression. For 
a variable that resolves to a field with an initializer, whether or not the 
field is {{final}}, 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, of {{final}} instance fields ({{a = b}}, {{b = c}}, {{c = 
a}}).
* A chain of fields that ends in a constant ({{static final String R = S}}, 
{{static final String S = /(a)/}}) is still followed and checked; for example, 
a bad group number is still reported.
* {{static final}} cycles are left out until GROOVY-12710 is fixed.

  was:
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.


> 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: 5.1.3, 6.0.0
>            Reporter: Paul King
>            Priority: Major
>
> 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 String R = S
>     static String S = R
>     static Pattern compile() { Pattern.compile(R) }
> }
> println 'compiled ' + Cycle.R
> {code}
> Actual:
> {noformat}
> 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 null}}, as it does 
> without the extension. The fields have no constant value to find, so there is 
> nothing to check.
> The same happens with {{FormatStringChecker}}, for example {{String.format(F, 
> 1)}} with {{static String F = G}} and {{static String G = F}}, and with 
> {{final}} instance fields ({{final String a = b}}, {{final String b = a}}).
> ||Version||RegexChecker||FormatStringChecker||
> |4.0.32|StackOverflowError|(checker not available)|
> |5.1.3|StackOverflowError|StackOverflowError|
> |6.0.0|StackOverflowError|StackOverflowError|
> The same cycle of {{static final}} fields overflows the compiler even without 
> these checkers; that is GROOVY-12710.
> h2. Cause
> {{CheckingVisitor.findConstExp}} finds the constant value of an expression. 
> For a variable that resolves to a field with an initializer, whether or not 
> the field is {{final}}, 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, of {{final}} instance fields ({{a = b}}, {{b = c}}, {{c = 
> a}}).
> * A chain of fields that ends in a constant ({{static final String R = S}}, 
> {{static final String S = /(a)/}}) is still followed and checked; for 
> example, a bad group number is still reported.
> * {{static final}} cycles are left out until GROOVY-12710 is fixed.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to