[
https://issues.apache.org/jira/browse/GROOVY-12709?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18139348#comment-18139348
]
ASF GitHub Bot commented on GROOVY-12709:
-----------------------------------------
testlens-app[bot] commented on PR #3239:
URL: https://github.com/apache/groovy/pull/3239#issuecomment-6675886954
## ✅ All tests passed ✅
🏷️ Commit: 470468b955dd809f5971841548710b72126d551e
▶️ Tests: 128300 executed
⚪️ Checks: 26/26 completed
---
_Learn more about TestLens at
[testlens.app/docs](https://testlens.app/docs/features/pr-comment/)._
> 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)