[stringtemplate-interest] [ST4 Beta-3] Endless loop when referencing argument in default value
Udo Borkowski
ub at abego-software.de
Mon Jan 31 14:07:12 PST 2011
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).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110131/993d2e88/attachment.html
More information about the stringtemplate-interest
mailing list