Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in ST.java line 245 #72

Closed
ghost opened this issue Mar 13, 2014 · 1 comment
Closed

Bug in ST.java line 245 #72

ghost opened this issue Mar 13, 2014 · 1 comment
Labels
Milestone

Comments

@ghost
Copy link

ghost commented Mar 13, 2014

The last line in this code snipette is from ST().add(...) is a bug.

It is possible for locals to have a length of zero, you can't assign EMPTY_ATTR to an array with zero length.

It means lines in my code like:

page.add("something", "value");

Fail with an array exception. I get some other array exceptions as well, but I don't know why. See below.

            arg = new FormalArgument(name);
            impl.addArg(arg);
            if ( locals==null ) locals = new Object[1];
            //else locals = Arrays.copyOf(locals, impl.formalArguments.size());
            else {
                Object[] copy = new Object[impl.formalArguments.size()];
                System.arraycopy(locals, 0, copy, 0,
                                 Math.min(locals.length, impl.formalArguments.size()));
                locals = copy;
            }
            locals[arg.index] = EMPTY_ATTR;

I also sometimes see this exception, no idea how to reproduce it though:

java.lang.ArrayIndexOutOfBoundsException: 4
 org.stringtemplate.v4.ST.add():246
 myapp.package.getPage():321
@kilidarria
Copy link

Under the situation that I found it was in the formal arguments at the top of the template.
If an argument in the header is duplicated ex key_protocol it is only entered in the formal arguments 2 times instead of 35 you have 34 arguments. when the formal arguments are added it must reinsert or overwrite the formal arguments. The index gets updated but the size remains the same, local is created based on the size not the index number then when it tries to do any processing it throws an outofbounds error
my_template( attib_1, attrib_2,attrib_3,attrib_2) :: <<
code
st.add("attrib_1","lah,blah) ;
st.add("attrib_2","blah,blah);
you now get an ArrayIndexOutOfBoundsException: 4 error

I see this all over the code new Object[impl.formalArguments.size()]

Actually not a bug unless your someone like me that spent the better part of 2 days resolving the issue but the issue could be trapped better such as throwing an error if the argument has already been added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants