8041998: RegExp implementation is not thread-safe

Reviewed-by: lagergren, sundar, attila
This commit is contained in:
Hannes Wallnöfer 2014-05-06 12:38:12 +02:00
parent 7579bb9eb4
commit 77834a008e
5 changed files with 67 additions and 27 deletions

View File

@ -46,9 +46,6 @@ public class JdkRegExp extends RegExp {
/** Java regexp pattern to use for match. We compile to one of these */
private Pattern pattern;
/** The matcher */
private RegExpMatcher matcher;
/**
* Construct a Regular expression from the given {@code source} and {@code flags} strings.
*
@ -95,14 +92,7 @@ public class JdkRegExp extends RegExp {
return null; // never matches or similar, e.g. a[]
}
RegExpMatcher currentMatcher = this.matcher;
if (currentMatcher == null || matcher.getInput() != str) {
currentMatcher = new DefaultMatcher(str);
this.matcher = currentMatcher;
}
return currentMatcher;
return new DefaultMatcher(str);
}
class DefaultMatcher implements RegExpMatcher {

View File

@ -44,9 +44,6 @@ public class JoniRegExp extends RegExp {
/** Compiled Joni Regex */
private Regex regex;
/** Matcher */
private RegExpMatcher matcher;
/**
* Construct a Regular expression from the given {@code pattern} and {@code flags} strings.
*
@ -95,14 +92,7 @@ public class JoniRegExp extends RegExp {
return null;
}
RegExpMatcher currentMatcher = this.matcher;
if (currentMatcher == null || input != currentMatcher.getInput()) {
currentMatcher = new JoniMatcher(input);
this.matcher = currentMatcher;
}
return currentMatcher;
return new JoniMatcher(input);
}
/**

View File

@ -131,12 +131,13 @@ public final class Regex implements RegexState {
this.warnings = null;
}
public void compile() {
public synchronized MatcherFactory compile() {
if (factory == null && analyser != null) {
Compiler compiler = new ArrayCompiler(analyser);
new ArrayCompiler(analyser).compile();
analyser = null; // only do this once
compiler.compile();
}
assert factory != null;
return factory;
}
public Matcher matcher(char[] chars) {
@ -144,8 +145,11 @@ public final class Regex implements RegexState {
}
public Matcher matcher(char[] chars, int p, int end) {
compile();
return factory.create(this, chars, p, end);
MatcherFactory matcherFactory = factory;
if (matcherFactory == null) {
matcherFactory = compile();
}
return matcherFactory.create(this, chars, p, end);
}
public WarnCallback getWarnings() {

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2010, 2014, 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-8041998: RegExp implementation is not thread-safe
*
* @test
* @run
*/
var Thread = java.lang.Thread;
function run() {
var line = 'content-type: text/html';
for (var i = 0; i < 300; i++) {
Thread.sleep(1);
line.split(/: /);
}
print("done");
}
var threads = [];
for (var i = 0; i < 4; i++) {
var thread = new Thread(run);
thread.start();
threads.push(thread);
}
for (var i = 0; i < 4; i++) {
threads[i].join();
}

View File

@ -0,0 +1,4 @@
done
done
done
done