[ 
https://issues.apache.org/jira/browse/GROOVY-12844?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152528#comment-18152528
 ] 

ASF GitHub Bot commented on GROOVY-12844:
-----------------------------------------

paulk-asert opened a new pull request, #3378:
URL: https://github.com/apache/groovy/pull/3378

   …lls a checked method more than once




> groovy-contracts: StackOverflowError when a contract calls a checked method 
> more than once
> ------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12844
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12844
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-contracts
>    Affects Versions: 6.0.0, 6.0.1
>            Reporter: Paul King
>            Priority: Major
>
> A class invariant that makes two calls to methods of its own class overflows 
> the stack:
> {code:groovy}
> import groovy.contracts.*
> @Invariant({ lo <= hi })
> class Range {
>     int getLo() { 1 }
>     int getHi() { 2 }
> }
> new Range()   // StackOverflowError
> {code}
> The same happens when one getter or method is called twice, e.g. {{v > 0 && v 
> < 9}}, and for a {{@Requires}} or {{@Ensures}} that calls its own method 
> twice, e.g. {{@Ensures({ m(1) > 0 && m(2) > 0 })}} on {{m}}. A single call 
> works, as do ordinary properties of the class ({{int lo = 1, hi = 2}}). The 
> invariant was tried in dynamic and statically compiled code, with the same 
> result. All of these fail on 6.0.0, 6.0.1 and master.
> h3. Trait properties, from 6.0.1
> Reading a trait property once is enough:
> {code:groovy}
> trait T { Integer count }
> @Invariant({ count == null || count >= 0 })
> class C implements T { }
> new C()   // 6.0.0: no error; 6.0.1: StackOverflowError
> {code}
> Two generated accessors are involved in one read ({{getCount}} and 
> {{T__count$get}}), and both check the invariant. On 6.0.0 the check in those 
> accessors wasn't run. Since GROOVY-12742 it is, which exposes this problem. 
> 6.0.0 didn't handle these invariants properly either: its accessors returned 
> null, so only an invariant that allows null passed.
> It's easy to meet on 6.0.1, since GROOVY-12741 advises moving a trait's 
> invariant to the implementing class.
> h3. Cause
> {{ContractExecutionTracker}} should stop a contract from checking itself 
> again while it runs. The code generated by 
> {{BaseGenerator.wrapAssertionBooleanExpression}} is, in effect:
> {code:java}
> try {
>     if (ContractExecutionTracker.track(...)) {
>         // evaluate the condition
>     }
> } finally {
>     ContractExecutionTracker.clear(...);
> }
> {code}
> A nested check is refused by {{track}}, but still runs {{clear}}, which 
> removes the outer check's entry. The next call from the same condition is 
> then no longer recognised as nested, and evaluates the condition again, 
> without end.
> h3. Possible fix
> Clear only what was tracked:
> {code:java}
> if (ContractExecutionTracker.track(...)) {
>     try {
>         // evaluate the condition
>     } finally {
>         ContractExecutionTracker.clear(...);
>     }
> }
> {code}
> With this change on master, all the forms above work, including the trait 
> properties, and violations are still reported, also on a second call after a 
> first violation.



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

Reply via email to