8175362: StringIndexOutOfBoundsException from /.*((a[^a]+){2})c$/.exec('ababc')
authorpmuthuswamy
Mon, 21 Aug 2017 11:33:52 +0530
changeset 46908 66e475363705
parent 46907 55232825577e
child 46909 23b82f4e4ed7
8175362: StringIndexOutOfBoundsException from /.*((a[^a]+){2})c$/.exec('ababc') Reviewed-by: sundar, hannesw Contributed-by: priya.muthuswamy@oracle.com
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/Analyser.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/Regex.java
nashorn/test/script/basic/JDK-8175362.js
nashorn/test/script/basic/JDK-8175362.js.EXPECTED
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/Analyser.java	Sun Aug 20 20:36:11 2017 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/Analyser.java	Mon Aug 21 11:33:52 2017 +0530
@@ -102,6 +102,8 @@
 
         env.memNodes = null;
 
+        new ArrayCompiler(this).compile();
+
         if (regex.numRepeat != 0 || regex.btMemEnd != 0) {
             regex.stackPopLevel = StackPopLevel.ALL;
         } else {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/Regex.java	Sun Aug 20 20:36:11 2017 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/Regex.java	Mon Aug 21 11:33:52 2017 +0530
@@ -127,31 +127,17 @@
         this.caseFoldFlag = caseFoldFlag;
         this.warnings = warnings;
 
-        this.analyser = new Analyser(new ScanEnvironment(this, syntax), chars, p, end);
-        this.analyser.compile();
+        new Analyser(new ScanEnvironment(this, syntax), chars, p, end).compile();
 
         this.warnings = null;
     }
 
-    public synchronized MatcherFactory compile() {
-        if (factory == null && analyser != null) {
-            new ArrayCompiler(analyser).compile();
-            analyser = null; // only do this once
-        }
-        assert factory != null;
-        return factory;
-    }
-
     public Matcher matcher(final char[] chars) {
         return matcher(chars, 0, chars.length);
     }
 
     public Matcher matcher(final char[] chars, final int p, final int end) {
-        MatcherFactory matcherFactory = factory;
-        if (matcherFactory == null) {
-            matcherFactory = compile();
-        }
-        return matcherFactory.create(this, chars, p, end);
+        return factory.create(this, chars, p, end);
     }
 
     public WarnCallback getWarnings() {
@@ -309,7 +295,6 @@
     }
 
     public String dumpByteCode() {
-        compile();
         return new ByteCodePrinter(this).byteCodeListToString();
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8175362.js	Mon Aug 21 11:33:52 2017 +0530
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8175362 : StringIndexOutOfBoundsException from /.*((a[^a]+){2})c$/.exec('ababc')
+ *
+ * @test
+ * @run
+ */
+
+var regexp;
+
+regexp = new RegExp(".*((a[^a]+){2})c$");
+print(regexp.exec("ababc"));
+regexp = new RegExp("(a[^a]+){3}");
+print(regexp.exec("ababc"));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8175362.js.EXPECTED	Mon Aug 21 11:33:52 2017 +0530
@@ -0,0 +1,2 @@
+ababc,abab,ab
+null