[stringtemplate-interest] [ST4 Beta-3] Endless loop when referencing argument in default value

Terence Parr parrt at cs.usfca.edu
Sat Feb 5 10:42:10 PST 2011


Oh right. i have to add a "lint mode" back in. adding to todo list.
Ter
On Jan 31, 2011, at 2:07 PM, Udo Borkowski wrote:

> Hi,
> 
> the following leads to an endless loop in ST4 Beta-3. 
> 
> t(s={<s>}) ::= <<
> <s>
> >>
> 
> main() ::= <<
> <t()>
> >>
> 
> It's a cyclic reference introduced by a default value expression referencing the argument it belongs to.
> 
> I added the following code to class ST to work around this issue:
> 
>     public static final int MAX_ST_CHAIN_LENGTH = 10000;
>     ...
>     public Object getAttribute(String name) {
>         ST p = this;
>         int i = MAX_ST_CHAIN_LENGTH;
>         while ( p!=null ) {
>             .
>             .
>             .
>             p = p.enclosingInstance;
>             
>             // check for cycles
>             i--;
>             if (i < 0) {
>                 throw new STException(
> 		         String.format("Cyclic reference detected when looking for attribute %s in template %s",
>                          name, p.getName()), null);
>             }
>         }
>         .
>         .
>         .
>     }
> 
> This breaks the cycle with an exception when a given limit for the iterations is reached.
> 
> 
> Udo
> 
> P.S.: The given example is the most simple template I could find to demonstrate the issue. Here the cycle is obvious. However it took me quite a while to find this bug because originally this was caused by a missing "}" for the default argument. The parser went on looking for the closing '}', also scanning the body of the template that (correctly) contained the <s> until it found the '}' in a following template. So it looked something like this, but more complicated: 
> 
> t(s={xyz) ::= "<s>"
> 
> otherTemplate(q={abc}) ::= << ... >>
> 
> P.P.S.: One can think of other ("more correct") ways to check for the cycle, like tracking the templates already handled in the loop, e.g. in a set. However the counter solution seems to require much less resources (time and space). 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110205/cffbfaaa/attachment.html 


More information about the stringtemplate-interest mailing list