You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2026/06/01 07:13:24 UTC

(tomcat) branch 10.1.x updated: Fix various minor Jasper issues from code review

This is an automated email from the ASF dual-hosted git repository.

rmaucher 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 309be50f1c Fix various minor Jasper issues from code review
309be50f1c is described below

commit 309be50f1c15e565774615a0909ecef2e863aeee
Author: remm <re...@apache.org>
AuthorDate: Mon Jun 1 09:11:32 2026 +0200

    Fix various minor Jasper issues from code review
---
 java/org/apache/jasper/compiler/ELParser.java      |  6 ++
 java/org/apache/jasper/compiler/JDTCompiler.java   |  3 +-
 java/org/apache/jasper/compiler/JavaCompiler.java  | 83 +++++++++++-----------
 java/org/apache/jasper/compiler/Node.java          |  2 +-
 java/org/apache/jasper/compiler/PageInfo.java      |  4 +-
 java/org/apache/jasper/compiler/SmapStratum.java   |  3 +-
 java/org/apache/jasper/compiler/SmapUtil.java      | 49 ++++++++-----
 .../apache/jasper/compiler/TagFileProcessor.java   |  7 +-
 .../jasper/resources/LocalStrings.properties       |  1 +
 9 files changed, 93 insertions(+), 65 deletions(-)

diff --git a/java/org/apache/jasper/compiler/ELParser.java b/java/org/apache/jasper/compiler/ELParser.java
index 4aab6fcd3e..e3f6023487 100644
--- a/java/org/apache/jasper/compiler/ELParser.java
+++ b/java/org/apache/jasper/compiler/ELParser.java
@@ -355,6 +355,7 @@ public class ELParser {
     private Token parseQuotedChars(char quote) {
         StringBuilder buf = new StringBuilder();
         buf.append(quote);
+        boolean foundQuote = false;
         while (hasNextChar()) {
             char ch = nextChar();
             if (ch == '\\') {
@@ -366,12 +367,17 @@ public class ELParser {
                             Localizer.getMessage("org.apache.jasper.compiler.ELParser.invalidQuoting", expression));
                 }
             } else if (ch == quote) {
+                foundQuote = true;
                 buf.append(ch);
                 break;
             } else {
                 buf.append(ch);
             }
         }
+        if (!foundQuote) {
+            throw new IllegalArgumentException(
+                    Localizer.getMessage("org.apache.jasper.compiler.ELParser.missingQuote", expression));
+        }
         return new QuotedString(getAndResetWhiteSpace(), buf.toString());
     }
 
diff --git a/java/org/apache/jasper/compiler/JDTCompiler.java b/java/org/apache/jasper/compiler/JDTCompiler.java
index 7590dca66c..55d46b69a2 100644
--- a/java/org/apache/jasper/compiler/JDTCompiler.java
+++ b/java/org/apache/jasper/compiler/JDTCompiler.java
@@ -106,7 +106,7 @@ public class JDTCompiler extends org.apache.jasper.compiler.Compiler {
 
             @Override
             public char[] getContents() {
-                char[] result = null;
+                char[] result;
                 try (FileInputStream is = new FileInputStream(sourceFile);
                         InputStreamReader isr = new InputStreamReader(is, ctxt.getOptions().getJavaEncoding());
                         Reader reader = new BufferedReader(isr)) {
@@ -120,6 +120,7 @@ public class JDTCompiler extends org.apache.jasper.compiler.Compiler {
                     buf.getChars(0, result.length, result, 0);
                 } catch (IOException ioe) {
                     log.error(Localizer.getMessage("jsp.error.compilation.source", sourceFile), ioe);
+                    result = new char[0];
                 }
                 return result;
             }
diff --git a/java/org/apache/jasper/compiler/JavaCompiler.java b/java/org/apache/jasper/compiler/JavaCompiler.java
index 85020a4391..86443ac761 100644
--- a/java/org/apache/jasper/compiler/JavaCompiler.java
+++ b/java/org/apache/jasper/compiler/JavaCompiler.java
@@ -57,55 +57,58 @@ public class JavaCompiler extends Compiler {
 
         javax.tools.JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
         DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
-        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null,
-                Charset.forName(ctxt.getOptions().getJavaEncoding()));
-        Iterable<? extends JavaFileObject> compilationUnits =
-                fileManager.getJavaFileObjectsFromFiles(List.of(new File(ctxt.getServletJavaFileName())));
-        // Perform Java compilation using the appropriate options
-        List<String> compilerOptions = List.of("-classpath", ctxt.getClassPath(), "-source",
-                ctxt.getOptions().getCompilerSourceVM(), "-target", ctxt.getOptions().getCompilerTargetVM());
-        Boolean result =
-                compiler.getTask(null, fileManager, diagnostics, compilerOptions, null, compilationUnits).call();
-
-        List<JavacErrorDetail> problemList = new ArrayList<>();
-        if (!result.booleanValue()) {
-            for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
-                if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
-                    try {
-                        problemList.add(ErrorDispatcher.createJavacError(diagnostic.getSource().getName(), pageNodes,
-                                new StringBuilder(diagnostic.getMessage(Locale.getDefault())),
-                                (int) diagnostic.getLineNumber(), ctxt));
-                    } catch (JasperException e) {
-                        log.error(Localizer.getMessage("jsp.error.compilation.jdtProblemError"), e);
+        try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null,
+                Charset.forName(ctxt.getOptions().getJavaEncoding()))) {
+
+            Iterable<? extends JavaFileObject> compilationUnits =
+                    fileManager.getJavaFileObjectsFromFiles(List.of(new File(ctxt.getServletJavaFileName())));
+            // Perform Java compilation using the appropriate options
+            List<String> compilerOptions = List.of("-classpath", ctxt.getClassPath(), "-source",
+                    ctxt.getOptions().getCompilerSourceVM(), "-target", ctxt.getOptions().getCompilerTargetVM());
+            Boolean result =
+                    compiler.getTask(null, fileManager, diagnostics, compilerOptions, null, compilationUnits).call();
+
+            List<JavacErrorDetail> problemList = new ArrayList<>();
+            if (!result.booleanValue()) {
+                for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
+                    if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
+                        try {
+                            problemList.add(ErrorDispatcher.createJavacError(diagnostic.getSource().getName(), pageNodes,
+                                    new StringBuilder(diagnostic.getMessage(Locale.getDefault())),
+                                    (int) diagnostic.getLineNumber(), ctxt));
+                        } catch (JasperException e) {
+                            log.error(Localizer.getMessage("jsp.error.compilation.jdtProblemError"), e);
+                        }
                     }
                 }
             }
-        }
 
-        if (!ctxt.keepGenerated()) {
-            File javaFile = new File(ctxt.getServletJavaFileName());
-            if (!javaFile.delete()) {
-                throw new JasperException(Localizer.getMessage("jsp.warning.compiler.javafile.delete.fail", javaFile));
+            if (!ctxt.keepGenerated()) {
+                File javaFile = new File(ctxt.getServletJavaFileName());
+                if (!javaFile.delete()) {
+                    throw new JasperException(Localizer.getMessage("jsp.warning.compiler.javafile.delete.fail", javaFile));
+                }
             }
-        }
 
-        if (!problemList.isEmpty()) {
-            JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]);
-            errDispatcher.javacError(jeds);
-        }
+            if (!problemList.isEmpty()) {
+                JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]);
+                errDispatcher.javacError(jeds);
+            }
 
-        if (log.isDebugEnabled()) {
-            long t2 = System.currentTimeMillis();
-            log.debug(Localizer.getMessage("jsp.compiled", ctxt.getServletJavaFileName(), Long.valueOf(t2 - t1)));
-        }
+            if (log.isDebugEnabled()) {
+                long t2 = System.currentTimeMillis();
+                log.debug(Localizer.getMessage("jsp.compiled", ctxt.getServletJavaFileName(), Long.valueOf(t2 - t1)));
+            }
 
-        if (ctxt.isPrototypeMode()) {
-            return;
-        }
+            if (ctxt.isPrototypeMode()) {
+                return;
+            }
+
+            // JSR45 Support
+            if (!options.isSmapSuppressed()) {
+                SmapUtil.installSmap(smaps);
+            }
 
-        // JSR45 Support
-        if (!options.isSmapSuppressed()) {
-            SmapUtil.installSmap(smaps);
         }
 
     }
diff --git a/java/org/apache/jasper/compiler/Node.java b/java/org/apache/jasper/compiler/Node.java
index 9834241a29..1c1e423e01 100644
--- a/java/org/apache/jasper/compiler/Node.java
+++ b/java/org/apache/jasper/compiler/Node.java
@@ -2885,7 +2885,7 @@ public abstract class Node implements TagConstants {
             Node n = null;
             try {
                 n = list.get(index);
-            } catch (ArrayIndexOutOfBoundsException e) {
+            } catch (IndexOutOfBoundsException e) {
                 // Ignore
             }
             return n;
diff --git a/java/org/apache/jasper/compiler/PageInfo.java b/java/org/apache/jasper/compiler/PageInfo.java
index a541b37997..9100102403 100644
--- a/java/org/apache/jasper/compiler/PageInfo.java
+++ b/java/org/apache/jasper/compiler/PageInfo.java
@@ -482,7 +482,9 @@ public class PageInfo {
      */
     public void popPrefixMapping(String prefix) {
         Deque<String> stack = xmlPrefixMapper.get(prefix);
-        stack.removeFirst();
+        if (stack != null) {
+            stack.removeFirst();
+        }
     }
 
     /**
diff --git a/java/org/apache/jasper/compiler/SmapStratum.java b/java/org/apache/jasper/compiler/SmapStratum.java
index 902e827293..68afa20a46 100644
--- a/java/org/apache/jasper/compiler/SmapStratum.java
+++ b/java/org/apache/jasper/compiler/SmapStratum.java
@@ -406,7 +406,8 @@ public class SmapStratum {
             }
 
             // This is the match
-            int inputOffset = (outputLineNumber - lineInfo.outputStartLine) / lineInfo.outputLineIncrement;
+            int inputOffset = (outputLineNumber - lineInfo.outputStartLine)
+                    / (lineInfo.outputLineIncrement == 0 ? 1 : lineInfo.outputLineIncrement);
 
             inputLineNumber = lineInfo.inputStartLine + inputOffset;
         }
diff --git a/java/org/apache/jasper/compiler/SmapUtil.java b/java/org/apache/jasper/compiler/SmapUtil.java
index 1fd4ba9737..852b471e68 100644
--- a/java/org/apache/jasper/compiler/SmapUtil.java
+++ b/java/org/apache/jasper/compiler/SmapUtil.java
@@ -713,10 +713,16 @@ public class SmapUtil {
             String fileName = lines[lineIndex].substring(i + 1);
             smapStratum.addFile(fileName, lines[++lineIndex]);
             lineIndex++;
+            if (lineIndex == lines.length) {
+                return null;
+            }
         }
 
         // Skip *L
         lineIndex++;
+        if (lineIndex == lines.length) {
+            return null;
+        }
 
         while (!lines[lineIndex].equals("*E")) {
             LineInfo li = new LineInfo();
@@ -746,6 +752,9 @@ public class SmapUtil {
             smapStratum.addLineInfo(li);
 
             lineIndex++;
+            if (lineIndex == lines.length) {
+                return null;
+            }
         }
 
         return smapStratum;
@@ -765,27 +774,29 @@ public class SmapUtil {
                 found = true;
             } else {
                 is = cl.getResourceAsStream(className.replace(".", "/") + ".class");
-                // Alternative approach would be to read the class file as per the
-                // JLS. That would require duplicating a lot of BCEL functionality.
-                int b = is.read();
-                while (b != -1) {
-                    if (b == 'S') {
-                        if ((b = is.read()) != 'M') {
-                            continue;
-                        }
-                        if ((b = is.read()) != 'A') {
-                            continue;
-                        }
-                        if ((b = is.read()) != 'P') {
-                            continue;
-                        }
-                        if ((b = is.read()) != '\n') {
-                            continue;
+                if (is != null) {
+                    // Alternative approach would be to read the class file as per the
+                    // JLS. That would require duplicating a lot of BCEL functionality.
+                    int b = is.read();
+                    while (b != -1) {
+                        if (b == 'S') {
+                            if ((b = is.read()) != 'M') {
+                                continue;
+                            }
+                            if ((b = is.read()) != 'A') {
+                                continue;
+                            }
+                            if ((b = is.read()) != 'P') {
+                                continue;
+                            }
+                            if ((b = is.read()) != '\n') {
+                                continue;
+                            }
+                            found = true;
+                            break;
                         }
-                        found = true;
-                        break;
+                        b = is.read();
                     }
-                    b = is.read();
                 }
             }
 
diff --git a/java/org/apache/jasper/compiler/TagFileProcessor.java b/java/org/apache/jasper/compiler/TagFileProcessor.java
index 9f22c137e3..e3fae2963f 100644
--- a/java/org/apache/jasper/compiler/TagFileProcessor.java
+++ b/java/org/apache/jasper/compiler/TagFileProcessor.java
@@ -19,6 +19,7 @@ package org.apache.jasper.compiler;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -644,12 +645,14 @@ public class TagFileProcessor {
      * @param classFileName If non-null, remove only the class file with this name.
      */
     public void removeProtoTypeFiles(String classFileName) {
-        for (Compiler c : tempVector) {
+        Iterator<Compiler> compilers = tempVector.iterator();
+        while (compilers.hasNext()) {
+            Compiler c = compilers.next();
             if (classFileName == null) {
                 c.removeGeneratedClassFiles();
             } else if (classFileName.equals(c.getCompilationContext().getClassFileName())) {
                 c.removeGeneratedClassFiles();
-                tempVector.remove(c);
+                compilers.remove();
                 return;
             }
         }
diff --git a/java/org/apache/jasper/resources/LocalStrings.properties b/java/org/apache/jasper/resources/LocalStrings.properties
index 4561aba9b4..07e7c9db5d 100644
--- a/java/org/apache/jasper/resources/LocalStrings.properties
+++ b/java/org/apache/jasper/resources/LocalStrings.properties
@@ -432,6 +432,7 @@ jstl.writerAfterOS=Cannot use a writer if an output stream has been used
 
 org.apache.jasper.compiler.ELParser.invalidQuotesForStringLiteral=The String literal [{0}] is not valid. It must be contained within single or double quotes.
 org.apache.jasper.compiler.ELParser.invalidQuoting=The expression [{0}] is not valid. Within a quoted String only [], [''] and ["] may be escaped with [].
+org.apache.jasper.compiler.ELParser.missingQuote=The expression [{0}] is not valid as it is missing an end quote.
 org.apache.jasper.compiler.TldCache.servletContextNull=The provided ServletContext was null
 org.apache.jasper.servlet.JasperInitializer.onStartup=Initializing Jasper for context [{0}]
 org.apache.jasper.servlet.TldScanner.webxmlAdd=Loading TLD for URI [{1}] from resource path [{0}]


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org