Bug 56529 - NoSuchElementException for attribute with empty string in custom tag
NoSuchElementException for attribute with empty string in custom tag
Status: RESOLVED FIXED
Product: Tomcat 7
Classification: Unclassified
Component: Jasper
trunk
PC All
: P2 normal (vote)
: ---
Assigned To: Tomcat Developers Mailing List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2014-05-15 16:33 UTC by Hariprasad Manchi
Modified: 2014-08-13 15:07 UTC (History)
0 users



Attachments
patch for Validator.java (888 bytes, patch)
2014-05-15 16:33 UTC, Hariprasad Manchi
Details | Diff
sample web application to see the exception. (8.96 KB, application/octet-stream)
2014-05-15 16:36 UTC, Hariprasad Manchi
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Hariprasad Manchi 2014-05-15 16:33:45 UTC
Created attachment 31628 [details]
patch for Validator.java

Hi team,
I was verifying the fix for https://issues.apache.org/bugzilla/show_bug.cgi?id=56481 and found one issue with respect to custom tag libraries.
A tld has an attribute which could be an empty string. Example is below. Here 'indicator' attribute is provided as "".
<dmf:requiredfieldvalidator 
	name='<%=Login.CONTROL_USERNAME_VALIDATOR%>' 
	controltovalidate='<%=Login.CONTROL_USERNAME%>' 
	nlsid='<%=Login.MSG_USERNAME_REQUIRED%>' 
	indicator=""
/>
However, while compiling this in jsp resulted in the following exception:

message Unable to compile class for JSP
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:579)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
root cause
java.util.NoSuchElementException
	java.util.ArrayList$Itr.next(ArrayList.java:834)
	org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1132)
	org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:879)
	org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1521)
	org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
	org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2413)
	org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2419)
	org.apache.jasper.compiler.Node$Root.accept(Node.java:464)
	org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
	org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1840)
	org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
note The full stack trace of the root cause is available in the Apache Tomcat/@VERSION@ logs.

By looking into the source of Validator.java I observed that for the empty string we get the ELNode iterator but there isn't a check to see if the element has next node or not. The call ((ELNode.Text) el.iterator().next()).getText(); results in the above exception.
I added a test to see if the element has next node and it worked fine.
Attached is the patch for your reference.

Regards,
Hariprasad
Comment 1 Hariprasad Manchi 2014-05-15 16:36:14 UTC
Created attachment 31629 [details]
sample web application to see the exception.

Try to load the loginex.jsp. You will see the exception.
Comment 2 Hariprasad Manchi 2014-05-15 16:40:17 UTC
I have also attached a sample web application to reproduce the issue.
Comment 3 Violeta Georgieva 2014-05-16 12:22:11 UTC
Thanks for the report and the patch. This has been fixed in trunk for 8.0.7 and in 7.0.x for 7.0.54 onwards.
Comment 4 Violeta Georgieva 2014-05-17 04:11:55 UTC
Small correction: the fix is in 8.0.8
Comment 5 rsanthakumar 2014-05-28 13:19:40 UTC
Hi,

I had the similar issues with the custom tag, even in the fixed code. It looks like the issue is fixed only in the method “checkXmlAttributes”, but there is similar kind of the implementation even in the “getJspAttribute” method of validator class.

I am not sure whether it is an intended implementation, but it is throwing the following exception during the JSP compilation. The application started working once we fix the code in “getJspAttribute” method

Exception:

java.util.NoSuchElementException
	at java.util.ArrayList$Itr.next(ArrayList.java:834)
	at org.apache.jasper.compiler.Validator$ValidateVisitor.getJspAttribute(Validator.java:1385)
	at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1262)
	at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:876)
	at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1538)

Code snippets from “getJSPAttribute”

1380                         if (el.containsEL()) {
1381                             validateFunctions(el, n);
1382                         } else {
1383                             // Get text with \$ and \# escaping removed.
1384                             // Should be a single Text node
1385                             value = ((ELNode.Text) el.iterator().next())
1386                                     .getText();
1387                             el = null;
1388                         }
1389                     }

Request you to please look into this. 

Thanks
Santhakumar
Comment 6 Violeta Georgieva 2014-05-28 14:49:57 UTC
(In reply to rsanthakumar from comment #5)
> Hi,
> 
> I had the similar issues with the custom tag, even in the fixed code. It
> looks like the issue is fixed only in the method “checkXmlAttributes”, but
> there is similar kind of the implementation even in the “getJspAttribute”
> method of validator class.
> 
This has been reported with bug 56561.
The fix will be available in 6.0.42, 7.0.55 and 8.0.9
Comment 7 Neale 2014-08-13 14:50:04 UTC
Can anyone identify which version the bug was introduced in pls.
Comment 8 Neale 2014-08-13 15:07:13 UTC
Found a 7.0.50 server to test on.  It's not broken in that version if that helps anyone.