changeset 46908:66e475363705

8175362: StringIndexOutOfBoundsException from /.*((a[^a]+){2})c$/.exec('ababc') Reviewed-by: sundar, hannesw Contributed-by: priya.muthuswamy@oracle.com
author pmuthuswamy
date Mon, 21 Aug 2017 11:33:52 +0530
parents 55232825577e
children 23b82f4e4ed7
files 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
diffstat 4 files changed, 42 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- 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