This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push:
new 010e73a6dc Remove the loop variable after the ForEach and ForTokens
tag plugins (#1158)
010e73a6dc is described below
commit 010e73a6dcb45ebc21d8eadc329b0630e6dd3e88
Author: Arturo Bernal <[email protected]>
AuthorDate: Wed Nov 18 12:50:01 2026 +0100
Remove the loop variable after the ForEach and ForTokens tag plugins (#1158)
The var attribute of c:forEach and c:forTokens has nested visibility, so it
must not be visible once the loop has ended. The standard tag handlers
remove
the variable from the page scope when the loop ends. The tag plugins set it
for each iteration and never removed it. After the loop the last value
stayed
in the page scope, where it hid an attribute of the same name in the
request,
session or application scope.
Remove the variable in a finally block after the loop, as the standard tag
handlers do, so that it is also removed if the body throws an exception.
---
.../jasper/tagplugins/jstl/core/ForEach.java | 26 ++++++++++++++++
.../jasper/tagplugins/jstl/core/ForTokens.java | 13 +++++++-
.../jasper/tagplugins/jstl/core/TestForEach.java | 23 ++++++++++++++
.../jasper/tagplugins/jstl/core/TestForTokens.java | 21 +++++++++++++
test/webapp/jstl/foreach-var-visibility.jsp | 35 ++++++++++++++++++++++
test/webapp/jstl/fortokens-var-visibility.jsp | 30 +++++++++++++++++++
6 files changed, 147 insertions(+), 1 deletion(-)
diff --git a/java/org/apache/jasper/tagplugins/jstl/core/ForEach.java
b/java/org/apache/jasper/tagplugins/jstl/core/ForEach.java
index e041ad6cf9..23f5bffd92 100644
--- a/java/org/apache/jasper/tagplugins/jstl/core/ForEach.java
+++ b/java/org/apache/jasper/tagplugins/jstl/core/ForEach.java
@@ -95,6 +95,9 @@ public final class ForEach implements TagPlugin {
LoopAttributeChecks.generateStepCheck(ctxt, stepName);
}
+ if (hasVar) {
+ ctxt.generateJavaSource("try {");
+ }
ctxt.generateJavaSource("for (int " + index + " = " + beginName + "; "
+ index + " <= " + endName + ";");
if (hasStep) {
ctxt.generateJavaSource(" " + index + " += " + stepName + ") {");
@@ -111,6 +114,9 @@ public final class ForEach implements TagPlugin {
}
ctxt.generateBody();
ctxt.generateJavaSource("}");
+ if (hasVar) {
+ generateRemoveVar(ctxt);
+ }
}
/**
@@ -152,6 +158,9 @@ public final class ForEach implements TagPlugin {
LoopAttributeChecks.generateStepCheck(ctxt, stepV);
}
+ if (hasVar) {
+ ctxt.generateJavaSource("try {");
+ }
// An empty range must not execute the body or consume any items.
if (hasBegin && hasEnd) {
ctxt.generateJavaSource("if (" + beginV + " <= " + endV + ") {");
@@ -250,6 +259,23 @@ public final class ForEach implements TagPlugin {
if (hasBegin && hasEnd) {
ctxt.generateJavaSource("}"); // Non-empty range
}
+ if (hasVar) {
+ generateRemoveVar(ctxt);
+ }
+ }
+
+
+ /**
+ * Close the {@code try} block that surrounds the loop and generate the
code that removes the loop variable. The
+ * variable has nested visibility so, as with the standard tag handler, it
is removed from the page scope when the
+ * loop ends, including when the body throws an exception.
+ */
+ private void generateRemoveVar(TagPluginContext ctxt) {
+ ctxt.generateJavaSource("} finally {");
+ ctxt.generateJavaSource("_jspx_page_context.removeAttribute(");
+ ctxt.generateAttribute("var");
+ ctxt.generateJavaSource(",
jakarta.servlet.jsp.PageContext.PAGE_SCOPE);");
+ ctxt.generateJavaSource("}");
}
/**
diff --git a/java/org/apache/jasper/tagplugins/jstl/core/ForTokens.java
b/java/org/apache/jasper/tagplugins/jstl/core/ForTokens.java
index 8e70b5dbfa..7b566ec2f5 100644
--- a/java/org/apache/jasper/tagplugins/jstl/core/ForTokens.java
+++ b/java/org/apache/jasper/tagplugins/jstl/core/ForTokens.java
@@ -111,7 +111,11 @@ public class ForTokens implements TagPlugin {
ctxt.generateJavaSource("int " + stepName + " = 1;");
}
- // the loop
+ // The loop. The variable has nested visibility so it is removed from
the page scope when the loop ends, including
+ // when the body throws an exception, as with the standard tag handler.
+ if (hasVar) {
+ ctxt.generateJavaSource("try {");
+ }
ctxt.generateJavaSource(
"for(int " + index + " = " + beginName + "; " + index + " <= "
+ endName + "; " + index + "++){");
ctxt.generateJavaSource(" String " + temp + " = " + stName +
".nextToken();");
@@ -124,6 +128,13 @@ public class ForTokens implements TagPlugin {
ctxt.generateBody();
ctxt.generateJavaSource(" }");
ctxt.generateJavaSource("}");
+ if (hasVar) {
+ String strVar = ctxt.getConstantAttribute("var");
+ ctxt.generateJavaSource("} finally {");
+ ctxt.generateJavaSource("
_jspx_page_context.removeAttribute(\"" + strVar +
+ "\", jakarta.servlet.jsp.PageContext.PAGE_SCOPE);");
+ ctxt.generateJavaSource("}");
+ }
}
}
diff --git a/test/org/apache/jasper/tagplugins/jstl/core/TestForEach.java
b/test/org/apache/jasper/tagplugins/jstl/core/TestForEach.java
index 3c9405b87e..a4eb705261 100644
--- a/test/org/apache/jasper/tagplugins/jstl/core/TestForEach.java
+++ b/test/org/apache/jasper/tagplugins/jstl/core/TestForEach.java
@@ -125,4 +125,27 @@ public class TestForEach extends AbstractTestTag {
Assert.assertTrue(res.toString(), res.toString()
.contains("jakarta.servlet.jsp.JspTagException: " + message));
}
+
+
+ /*
+ * The loop variable has nested visibility so it must not be visible after
the loop and must not hide an attribute
+ * in another scope.
+ */
+ @Test
+ public void testVarNotVisibleAfterLoop() throws Exception {
+ ByteChunk res = new ByteChunk();
+
+ int rc = getUrl("http://localhost:" + getPort() +
+ "/test/jstl/foreach-var-visibility.jsp", res, null);
+
+ Assert.assertEquals(HttpServletResponse.SC_OK, rc);
+
+ String body = res.toString();
+ Assert.assertTrue(body, body.contains("items=a"));
+ Assert.assertTrue(body, body.contains("items=b"));
+ Assert.assertTrue(body, body.contains("afterItems=outerI"));
+ Assert.assertTrue(body, body.contains("range=2"));
+ Assert.assertTrue(body, body.contains("afterRange=outerJ"));
+ Assert.assertTrue(body, body.contains("afterNone=</p>"));
+ }
}
diff --git a/test/org/apache/jasper/tagplugins/jstl/core/TestForTokens.java
b/test/org/apache/jasper/tagplugins/jstl/core/TestForTokens.java
index 7c1aa63e44..1dff11d889 100644
--- a/test/org/apache/jasper/tagplugins/jstl/core/TestForTokens.java
+++ b/test/org/apache/jasper/tagplugins/jstl/core/TestForTokens.java
@@ -55,4 +55,25 @@ public class TestForTokens extends AbstractTestTag {
Assert.assertTrue(res.toString(), res.toString()
.contains("jakarta.servlet.jsp.JspTagException: " + message));
}
+
+
+ /*
+ * The loop variable has nested visibility so it must not be visible after
the loop and must not hide an attribute
+ * in another scope.
+ */
+ @Test
+ public void testVarNotVisibleAfterLoop() throws Exception {
+ ByteChunk res = new ByteChunk();
+
+ int rc = getUrl("http://localhost:" + getPort() +
+ "/test/jstl/fortokens-var-visibility.jsp", res, null);
+
+ Assert.assertEquals(HttpServletResponse.SC_OK, rc);
+
+ String body = res.toString();
+ Assert.assertTrue(body, body.contains("token=a"));
+ Assert.assertTrue(body, body.contains("token=b"));
+ Assert.assertTrue(body, body.contains("afterTokens=outerT"));
+ Assert.assertTrue(body, body.contains("afterNone=</p>"));
+ }
}
diff --git a/test/webapp/jstl/foreach-var-visibility.jsp
b/test/webapp/jstl/foreach-var-visibility.jsp
new file mode 100644
index 0000000000..75719ce3d9
--- /dev/null
+++ b/test/webapp/jstl/foreach-var-visibility.jsp
@@ -0,0 +1,35 @@
+<%--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+--%>
+<%@ page session="false" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%
+ request.setAttribute("i", "outerI");
+ request.setAttribute("j", "outerJ");
+%>
+<c:forEach items="${['a','b']}" var="i">
+ <p>items=${i}</p>
+</c:forEach>
+<p>afterItems=${i}</p>
+<c:forEach begin="1" end="2" var="j">
+ <p>range=${j}</p>
+</c:forEach>
+<p>afterRange=${j}</p>
+<c:forEach items="${['a','b']}" var="none">
+ <p>none=${none}</p>
+</c:forEach>
+<p>afterNone=${none}</p>
diff --git a/test/webapp/jstl/fortokens-var-visibility.jsp
b/test/webapp/jstl/fortokens-var-visibility.jsp
new file mode 100644
index 0000000000..b709905dec
--- /dev/null
+++ b/test/webapp/jstl/fortokens-var-visibility.jsp
@@ -0,0 +1,30 @@
+<%--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+--%>
+<%@ page session="false" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%
+ request.setAttribute("t", "outerT");
+%>
+<c:forTokens items="a,b" delims="," var="t">
+ <p>token=${t}</p>
+</c:forTokens>
+<p>afterTokens=${t}</p>
+<c:forTokens items="a,b" delims="," var="none">
+ <p>none=${none}</p>
+</c:forTokens>
+<p>afterNone=${none}</p>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]