[stringtemplate-interest] ST4: Possible NullPointerException or redundant check in CompiledST.getTemplateRange

Terence Parr parrt at cs.usfca.edu
Mon Apr 4 17:04:32 PDT 2011


good point. it can't be null really ever. removed.
Ter
On Mar 31, 2011, at 2:29 AM, Udo Borkowski wrote:

> CompiledST contains this method:
> 
> 	public Interval getTemplateRange() {
> 		if ( isAnonSubtemplate ) {
> 			Interval start = sourceMap[0];
> 			Interval stop = null;
> 			for (int i = sourceMap.length-1; i>=0; i--) {
> 				Interval I = sourceMap[i];
> 				if ( I!=null ) {
> 					stop = I;
> 					break;
> 				}
> 			}
> 			if ( template!=null ) return new Interval(start.a, stop.b); // (1)
> 		}
> 		return new Interval(0, template.length()-1); // (2)
> 	}
> 
> Assume isAnonSubtemplate is true.
> 
> At (1) there is a check for template!=null . 
> I.e. when template is not null the method returns at (1).
> When template is null the execution continues at (2).
> 
> However the statement at (2) contains the expression template.length() , i.e. it access the template member. As template is null this will lead to a NullPointerException.
> 
> I haven't run into this NPE yet. Maybe template is never null when isAnonSubtemplate is true. In that case the check 
> 
> 	if ( template!=null ) 
> 
> is redundant and can be removed.
> 
> If template can be null when  isAnonSubtemplate is true the code at (2) needs to be changed to avoid the NPE.
> 
> 
> Udo
> 
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list