groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: GROOVY-9329: OOME raised while `parseClass(scriptText)` (#1117)
Date Thu, 12 Dec 2019 23:35:41 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 635039e  GROOVY-9329: OOME raised while `parseClass(scriptText)` (#1117)
635039e is described below

commit 635039e03e71b361931e77e7328baff9481d85cd
Author: Daniel.Sun <sunlan@apache.org>
AuthorDate: Fri Dec 13 07:35:32 2019 +0800

    GROOVY-9329: OOME raised while `parseClass(scriptText)` (#1117)
---
 src/main/java/groovy/lang/GroovyClassLoader.java | 10 ++++--
 src/test/groovy/bugs/Groovy9329.groovy           | 42 ++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/src/main/java/groovy/lang/GroovyClassLoader.java b/src/main/java/groovy/lang/GroovyClassLoader.java
index cd44d74..abc6da0 100644
--- a/src/main/java/groovy/lang/GroovyClassLoader.java
+++ b/src/main/java/groovy/lang/GroovyClassLoader.java
@@ -26,6 +26,7 @@
 package groovy.lang;
 
 import groovy.util.CharsetToolkit;
+import org.codehaus.groovy.GroovyBugError;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.FieldNode;
@@ -115,7 +116,7 @@ public class GroovyClassLoader extends URLClassLoader {
                         URL ret = getSourceFile(filename, extension);
                         if (ret != null)
                             return ret;
-                    } catch (Throwable t) { //
+                    } catch (Throwable t) {
                     }
                 }
                 return null;
@@ -263,8 +264,11 @@ public class GroovyClassLoader extends URLClassLoader {
      * @return the main class defined in the given script
      */
     public Class parseClass(String text) throws CompilationFailedException {
-        return parseClass(text, "script" + System.currentTimeMillis() +
-                Math.abs((long) text.hashCode()) + ".groovy");
+        try {
+            return parseClass(text, "Script_" + EncodingGroovyMethods.md5(text) + ".groovy");
+        } catch (NoSuchAlgorithmException e) {
+            throw new GroovyBugError("Failed to generate md5", e); // should never happen
+        }
     }
 
     public synchronized String generateScriptName() {
diff --git a/src/test/groovy/bugs/Groovy9329.groovy b/src/test/groovy/bugs/Groovy9329.groovy
new file mode 100644
index 0000000..00cdce7
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9329.groovy
@@ -0,0 +1,42 @@
+/*
+ *  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 groovy.bugs
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+@CompileStatic
+final class Groovy9329 {
+    @Test
+    void test() {
+        assertScript '''
+            def gcl = new GroovyClassLoader()
+            def scriptText = 'def x = 1'
+            gcl.parseClass(scriptText)
+            
+            def begin = gcl.@classCache.size()
+            3.times { gcl.parseClass(scriptText) }
+            def end = gcl.@classCache.size()
+            
+            assert end == begin
+        '''
+    }
+}


Mime
View raw message