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
commit e54527e0d47da208403e0daa7a568e22ffc3b550 Author: opencode <[email protected]> AuthorDate: Sun Oct 18 21:34:37 2026 +0200 Avoid NullPointerException when the JSP source cannot be read for an error detail When a compilation error is reported, JavacErrorDetail reads the JSP source to include a fragment of the page in the error report. The JSP input stream can legitimately be null, for example when the tag file entry is missing from its jar or when the JSP is removed between translation and compilation. In that case reading the stream threw a NullPointerException, which escaped the error detail construction and replaced the compilation error report with an opaque failure. Fall back to reporting only the error in the generated Java source when the JSP source cannot be obtained, as is already done when the error cannot be mapped to a JSP line at all. --- .../apache/jasper/compiler/JavacErrorDetail.java | 6 +++ .../jasper/compiler/TestJavacErrorDetail.java | 52 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/java/org/apache/jasper/compiler/JavacErrorDetail.java b/java/org/apache/jasper/compiler/JavacErrorDetail.java index c8a7c58cd7..eef5f57205 100644 --- a/java/org/apache/jasper/compiler/JavacErrorDetail.java +++ b/java/org/apache/jasper/compiler/JavacErrorDetail.java @@ -83,6 +83,12 @@ public class JavacErrorDetail { } else { is = ctxt.getResourceAsStream(jspFileName); } + if (is == null) { + // The JSP source is no longer available. Report only the + // details of the error in the generated Java source. + this.jspBeginLineNum = -1; + return; + } // Read both files in, so we can inspect them String[] jspLines = readFile(is); diff --git a/test/org/apache/jasper/compiler/TestJavacErrorDetail.java b/test/org/apache/jasper/compiler/TestJavacErrorDetail.java new file mode 100644 index 0000000000..edc567215b --- /dev/null +++ b/test/org/apache/jasper/compiler/TestJavacErrorDetail.java @@ -0,0 +1,52 @@ +/* + * 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. + */ +package org.apache.jasper.compiler; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.jasper.JspCompilationContext; +import org.easymock.EasyMock; + +public class TestJavacErrorDetail { + + /* + * If the JSP source is no longer available when the error detail is + * constructed, such as when the JSP is removed between translation and + * compilation, the error detail must fall back to reporting only the + * error in the generated Java source, rather than throwing a + * NullPointerException. + */ + @Test + public void testMissingJspSource() throws Exception { + JspCompilationContext ctxt = EasyMock.createMock( + JspCompilationContext.class); + EasyMock.expect(ctxt.getTagFileJar()).andReturn(null).anyTimes(); + EasyMock.expect(ctxt.getResourceAsStream("/a.jsp")).andReturn(null) + .anyTimes(); + EasyMock.replay(ctxt); + + JavacErrorDetail detail = new JavacErrorDetail("a_jsp.java", 10, + "/a.jsp", 5, new StringBuilder("error message"), ctxt); + + // A negative JSP line number signals that no JSP line is known, in + // which case only the Java source error details are reported + Assert.assertEquals(-1, detail.getJspBeginLineNumber()); + + EasyMock.verify(ctxt); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
