Skip to content

Commit 302e113

Browse files
committed
% is only escaped as <\%, not \% git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1214855 13f79535-47bb-0310-9956-ffa450edef68
1 parent de5d8e3 commit 302e113

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

java/org/apache/jasper/compiler/Parser.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ private boolean parseCustomTag(Node parent) throws JasperException {
12781278

12791279
/*
12801280
* Parse for a template text string until '<' or "${" or "#{" is encountered,
1281-
* recognizing escape sequences "\%", "\$", and "\#".
1281+
* recognizing escape sequences "<\%", "\$", and "\#".
12821282
*/
12831283
private void parseTemplateText(Node parent) throws JasperException {
12841284

@@ -1297,8 +1297,33 @@ private void parseTemplateText(Node parent) throws JasperException {
12971297
while (reader.hasMoreInput()) {
12981298
ch = reader.nextChar();
12991299
if (ch == '<') {
1300-
reader.pushChar();
1301-
break;
1300+
// Check for <\%
1301+
ch = reader.nextChar();
1302+
if (ch == -1) {
1303+
reader.pushChar();
1304+
break;
1305+
} else if (ch == '\\') {
1306+
ch = reader.nextChar();
1307+
if (ch == -1) {
1308+
reader.pushChar();
1309+
reader.pushChar();
1310+
break;
1311+
} else if (ch == '%') {
1312+
ttext.write('<');
1313+
ttext.write('\\');
1314+
ttext.write('%');
1315+
continue;
1316+
} else {
1317+
reader.pushChar();
1318+
reader.pushChar();
1319+
reader.pushChar();
1320+
break;
1321+
}
1322+
} else {
1323+
reader.pushChar();
1324+
reader.pushChar();
1325+
break;
1326+
}
13021327
} else if ((ch == '$' || ch == '#') && !pageInfo.isELIgnored()) {
13031328
if (!reader.hasMoreInput()) {
13041329
ttext.write(ch);
@@ -1318,9 +1343,9 @@ private void parseTemplateText(Node parent) throws JasperException {
13181343
break;
13191344
}
13201345
char next = (char) reader.peekChar();
1321-
// Looking for \% or \$ or \#
1322-
if (next == '%' || ((next == '$' || next == '#') &&
1323-
!pageInfo.isELIgnored())) {
1346+
// Looking for \$ or \# when EL is being used
1347+
if ((next == '$' || next == '#') &&
1348+
!pageInfo.isELIgnored()) {
13241349
ch = reader.nextChar();
13251350
}
13261351
}

test/org/apache/jasper/compiler/TestParser.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,26 @@ public void testBug49297Tag() throws Exception {
290290
assertEcho(res.toString(), "OK");
291291
}
292292

293+
@Test
294+
public void testBug52335() throws Exception {
295+
Tomcat tomcat = getTomcatInstance();
296+
297+
File appDir =
298+
new File("test/webapp-3.0");
299+
// app dir is relative to server home
300+
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
301+
302+
tomcat.start();
303+
304+
ByteChunk res = getUrl("http://localhost:" + getPort() +
305+
"/test/bug52335.jsp");
306+
307+
String result = res.toString();
308+
// Beware of the differences between escaping in JSP attributes and
309+
// in Java Strings
310+
assertEcho(result, "00 - \\% \\\\% <\\%");
311+
}
312+
293313
/** Assertion for text printed by tags:echo */
294314
private static void assertEcho(String result, String expected) {
295315
assertTrue(result.indexOf("<p>" + expected + "</p>") > 0);

test/webapp-3.0/bug52335.jsp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<%--
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
--%>
17+
<%@ page isELIgnored="true" %>
18+
<html>
19+
<head><title>Bug 52335 test case</title></head>
20+
<body>
21+
<p>00 - \% \\% <\%</p>
22+
</body>
23+
</html>
24+

0 commit comments

Comments
 (0)