Re: Unexpected exception calling VelocityEngine.mergeTemplate when using 2.4 or 2.4.1

Claude Brisson <[email protected]> Thu, 12 Jun 2025 09:23:16 +0200
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
 From what I found, the problem happens at the very end of the template. 
It seems to be a corner case where the parser doesn't have the start 
position of the very last token. So it will be useful to know how the 
faulty template ends (with a reference, a directive, etc.?).

Can you identify the faulty template, and tell us what is at the end?

On 12/06/2025 08:27, Claude Brisson wrote:
> Since it happens during the parsing, it means that it only depends on 
> the template itself, not to the values provided to it.
>
> So if you are able to log the template location, you will know the 
> problematic template at least.
>
> I'm going to try to pinpoint the problem with the stack trace you 
> provided and let you know if I am able to identify the cause.
>
> On 10/06/2025 23:50, Kish, Robert [AG] wrote:
>> Hello!
>>
>> We have 2 similar environments and under Velocity Engine 2.3, the 
>> code works as expected and we get back the template from the API call.
>> But when upgrading to 2.4.1 (or trying 2.4) one of our environments 
>> fails with an exception. Unfortunately, it's in an environment where 
>> I can't use a debugger.
>>
>> I'm looking for ideas on how to resolve this issue or identify a 
>> specific bug that can be reported to the Velocity Team for resolution.
>>
>> Log + Exception
>> o.a.v.macro - autoload off: VM system will not automatically reload 
>> global library macros
>> o.a.v.parser - XXXXXXXXXXXXXXXXXXXXXXXXXXXX: offset -1, count 79, 
>> length 4096
>> java.lang.StringIndexOutOfBoundsException: offset -1, count 79, 
>> length 4096
>>          at 
>> java.base/java.lang.String.checkBoundsOffCount(String.java:3304) ~[?:?]
>>          at java.base/java.lang.String.rangeCheck(String.java:280) 
>> ~[?:?]
>>          at java.base/java.lang.String.<init>(String.java:276) ~[?:?]
>>                 at 
>> org.apache.velocity.runtime.parser.VelocityCharStream.GetImage(VelocityCharStream.java:434) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParserTokenManager.jjFillToken(StandardParserTokenManager.java:7796) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParserTokenManager.getNextToken(StandardParserTokenManager.java:7866) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParser.jj_scan_token(StandardParser.java:5611) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParser.jj_3R_EndingZeroWidthWhitespace_1591_5_49(StandardParser.java:5448) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParser.jj_3_3(StandardParser.java:5454) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParser.jj_2_3(StandardParser.java:3886) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParser.Statement(StandardParser.java:654) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParser.process(StandardParser.java:495) 
>> ~[velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.parser.StandardParser.parse(StandardParser.java:184) 
>> [velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.RuntimeInstance.parse(RuntimeInstance.java:1344) 
>> [velocity-engine-core-2.4.jar:2.4]
>>          at org.apache.velocity.Template.process(Template.java:145) 
>> [velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:429) 
>> [velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:346) 
>> [velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1694) 
>> [velocity-engine-core-2.4.jar:2.4]
>>          at 
>> org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:284) 
>> [velocity-engine-core-2.4.jar:2.4]
>>
>> Environment that throws the exception:
>> Linux / Tomcat
>>
>> Server version name:   Apache Tomcat/9.0.104
>> Server built:          Apr 4 2025 12:32:55 UTC
>> Server version number: 9.0.104.0
>> OS Name:               Linux
>> OS Version:            4.14.355-277.647.amzn2.x86_64
>> Architecture:          amd64
>> Java Home: /usr/lib/jvm/java-11-amazon-corretto.x86_64
>> JVM Version:           11.0.27+6-LTS
>> JVM Vendor:            Amazon.com Inc.
>>
>>
>> Environment that does not throw the exception:
>> Windows / TomEE
>>
>> INFO: Server version name:   Apache Tomcat (TomEE)/9.0.82 (8.0.16)
>> INFO: Server built:          Oct 11 2023 13:15:08 UTC
>> INFO: Server version number: 9.0.82.0
>> INFO: OS Name:               Windows 11
>> INFO: OS Version:            10.0
>> INFO: Architecture:          amd64
>> INFO: Java Home:             C:\Program Files\Amazon 
>> Corretto\jdk11.0.26_4
>> INFO: JVM Version:           11.0.26+4-LTS
>> INFO: JVM Vendor:            Amazon.com Inc.
>>
>> Robert Kish (Consultant) | Application Developer, IT Application 
>> Services | NJ Dept. of Agriculture
>>
>> CONFIDENTIALITY NOTICE: The information contained in this 
>> communication from New Jersey Department of Agriculture is privileged 
>> and confidential and is intended for the sole use of the persons or 
>> entities who are the addressees. If you are not an intended recipient 
>> of this email, the dissemination, distribution, copying or use of the 
>> information it contains is strictly prohibited. If you have received 
>> this communication in error, please immediately contact the sender of 
>> this email message to arrange for the return of this information.
>>