Skip to content

Commit de62e2b

Browse files
daschlMichael Nitschinger
authored andcommitted
SPY-170: Fix concurrency issue in StringUtils.isJSONObject()
Motivation ---------- A concurrency issue was reported in the StringUtils class, pointing towards the pattern matching of the JSON string. Modifications ------------- It turned out that the matcher is not thread safe and there is a race condition between clearing it and then matching on it. By falling back to the thread-safe builder directly the race condition goes away. Result ------ No concurrency issue anymore in the helper method. Change-Id: I09729af78bd241ff8dcb0869992d3ff474fb6ec1 Reviewed-on: http://review.couchbase.org/37603 Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com> Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
1 parent b72840b commit de62e2b

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

src/main/java/net/spy/memcached/util/StringUtils.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.util.Collection;
2929
import java.util.Iterator;
30-
import java.util.regex.Matcher;
3130
import java.util.regex.Pattern;
3231

3332
/**
@@ -40,11 +39,6 @@ public final class StringUtils {
4039
*/
4140
private static final Pattern decimalPattern = Pattern.compile("^-?\\d+$");
4241

43-
/**
44-
* The matcher for the decimal pattern regex.
45-
*/
46-
private static final Matcher decimalMatcher = decimalPattern.matcher("");
47-
4842
/**
4943
* Maximum supported key length.
5044
*/
@@ -55,7 +49,7 @@ public final class StringUtils {
5549
*/
5650
private static final IllegalArgumentException KEY_TOO_LONG_EXCEPTION =
5751
new IllegalArgumentException("Key is too long (maxlen = "
58-
+ MAX_KEY_LENGTH + ")");
52+
+ MAX_KEY_LENGTH + ')');
5953

6054
/**
6155
* Exception thrown if the input key is empty.
@@ -112,7 +106,7 @@ public static boolean isJsonObject(final String s) {
112106

113107
if (s.startsWith("{") || s.startsWith("[")
114108
|| "true".equals(s) || "false".equals(s)
115-
|| "null".equals(s) || decimalMatcher.reset(s).matches()) {
109+
|| "null".equals(s) || decimalPattern.matcher(s).matches()) {
116110
return true;
117111
}
118112

0 commit comments

Comments
 (0)