You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/07/03 23:39:31 UTC

[groovy] branch GROOVY_4_0_X updated: GROOVY-10676: ErrorReporter (used by FileSystemCompiler) can throw an IOOB exception on files with CR only

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

paulk pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new 72dcc71f3c GROOVY-10676: ErrorReporter (used by FileSystemCompiler) can throw an IOOB exception on files with CR only
72dcc71f3c is described below

commit 72dcc71f3c2a7b1cbf20d92f6c877e147a533575
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Jul 3 23:18:44 2022 +1000

    GROOVY-10676: ErrorReporter (used by FileSystemCompiler) can throw an IOOB exception on files with CR only
---
 .../org/codehaus/groovy/control/SourceUnit.java    |  2 ++
 src/test/groovy/bugs/Groovy10676.groovy            | 36 ++++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/control/SourceUnit.java b/src/main/java/org/codehaus/groovy/control/SourceUnit.java
index 2fc4460d17..f62f857709 100644
--- a/src/main/java/org/codehaus/groovy/control/SourceUnit.java
+++ b/src/main/java/org/codehaus/groovy/control/SourceUnit.java
@@ -283,6 +283,8 @@ public class SourceUnit extends ProcessingUnit {
                 if (column > 40) {
                     int start = column - 30 - 1;
                     int end = (column + 10 > text.length() ? text.length() : column + 10 - 1);
+                    if (start >= text.length() || end < start)
+                        return null; // can happen with CR only files GROOVY-10676
                     sample = "   " + text.substring(start, end) + Utilities.eol() + "   " +
                             marker.substring(start);
                 } else {
diff --git a/src/test/groovy/bugs/Groovy10676.groovy b/src/test/groovy/bugs/Groovy10676.groovy
new file mode 100644
index 0000000000..e5f22ee30b
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10676.groovy
@@ -0,0 +1,36 @@
+/*
+ *  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 bugs
+
+import org.codehaus.groovy.tools.ErrorReporter
+import org.junit.Test
+
+class Groovy10676 {
+    @Test
+    void test() {
+        try {
+            // error should be at column > 40, first line should be short
+            new GroovyShell().parse('/*\r * some comment\r */\r           class class {}\r')
+        } catch(e) {
+            Writer data = new StringWriter()
+            new ErrorReporter(e, false).write(new PrintWriter(data))
+            assert data.toString().contains("Unexpected input: 'class'")
+        }
+    }
+}