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

rmaucher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 187c84bd15 Fix various minor Jasper issues from code review
187c84bd15 is described below

commit 187c84bd1528c472ff93c0a5043ef616d5ad00e7
Author: remm <[email protected]>
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 ea7a0a314a..7673d95606 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 2068df7360..a5d42dbb4b 100644
--- a/java/org/apache/jasper/compiler/JDTCompiler.java
+++ b/java/org/apache/jasper/compiler/JDTCompiler.java
@@ -105,7 +105,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)) {
@@ -119,6 +119,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 a58d586fe5..030e35665f 100644
--- a/java/org/apache/jasper/compiler/Node.java
+++ b/java/org/apache/jasper/compiler/Node.java
@@ -2796,7 +2796,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 2129f5e3c1..cf0afb6f7e 100644
--- a/java/org/apache/jasper/compiler/PageInfo.java
+++ b/java/org/apache/jasper/compiler/PageInfo.java
@@ -480,7 +480,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 e3e745469f..052d1f43a4 100644
--- a/java/org/apache/jasper/compiler/SmapUtil.java
+++ b/java/org/apache/jasper/compiler/SmapUtil.java
@@ -707,10 +707,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();
@@ -740,6 +746,9 @@ public class SmapUtil {
             smapStratum.addLineInfo(li);
 
             lineIndex++;
+            if (lineIndex == lines.length) {
+                return null;
+            }
         }
 
         return smapStratum;
@@ -759,27 +768,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 ea50a5da0d..d2cf7e8aa5 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 661be6a747..c9d4215a35 100644
--- a/java/org/apache/jasper/resources/LocalStrings.properties
+++ b/java/org/apache/jasper/resources/LocalStrings.properties
@@ -418,6 +418,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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to