Skip to content

Commit 386a843

Browse files
daschlMichael Nitschinger
authored andcommitted
SPY-168: isJSONObject should handle null or empty input values.
Motivation ---------- Before this change, the isJSONObject was suspect to NPEs and possibly also regex match failings on empty strings, although that does not seem to be the case on Java 1.7+. Modifications ------------- Specific null and empty checks have been added to make the method more robust. Result ------ NPEs are not raised anymore and also empty strings are checked much quicker. Test cases have been added to verify functionality. Side note: An exception like this was reported on empty strings, but could not be verified with Java 1.7 onward. It is suspected that this changeset also fixes the issue given the input type and code process: Original Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 0 Stack trace: java.lang.String.charAt(String.java:658) java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3715) java.util.regex.Pattern$Ques.match(Pattern.java:4079) java.util.regex.Pattern$Begin.match(Pattern.java:3472) java.util.regex.Matcher.match(Matcher.java:1221) java.util.regex.Matcher.matches(Matcher.java:559) net.spy.memcached.util.StringUtils.isJsonObject(StringUtils.java:109) net.spy.memcached.transcoders.SerializingTranscoder.encode(Seriali ... Change-Id: Icaca2f3fef658cdb2c376b6b4dba12ae0327f240 Reviewed-on: http://review.couchbase.org/36695 Reviewed-by: Matt Ingenthron <matt@couchbase.com> Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com> Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
1 parent 68daf93 commit 386a843

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
package net.spy.memcached.util;
2424

25+
import net.spy.memcached.KeyUtil;
26+
import net.spy.memcached.MemcachedClientIF;
27+
2528
import java.util.Collection;
2629
import java.util.Iterator;
2730
import java.util.regex.Matcher;
2831
import java.util.regex.Pattern;
2932

30-
import net.spy.memcached.KeyUtil;
31-
import net.spy.memcached.MemcachedClientIF;
32-
3333
/**
3434
* Utility methods on string objects.
3535
*/
@@ -106,6 +106,10 @@ public static String join(final Collection<String> chunks,
106106
* @return true if it is a JSON object, false otherwise.
107107
*/
108108
public static boolean isJsonObject(final String s) {
109+
if (s == null || s.isEmpty()) {
110+
return false;
111+
}
112+
109113
if (s.startsWith("{") || s.startsWith("[")
110114
|| "true".equals(s) || "false".equals(s)
111115
|| "null".equals(s) || decimalMatcher.reset(s).matches()) {

src/test/java/net/spy/memcached/util/StringUtilsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ public void shouldValidateNonJSONObject() {
7575
assertFalse(StringUtils.isJsonObject("1,244.0"));
7676
}
7777

78+
@Test
79+
public void shouldWorkWithEmptyOrNullStrings() {
80+
assertFalse(StringUtils.isJsonObject(""));
81+
assertFalse(StringUtils.isJsonObject(null));
82+
assertFalse(StringUtils.isJsonObject("\r\n"));
83+
assertFalse(StringUtils.isJsonObject("\n"));
84+
assertFalse(StringUtils.isJsonObject(" "));
85+
}
86+
7887
@Test
7988
public void shouldValidateAsciiKey() {
8089
StringUtils.validateKey("mykey1234", false);

0 commit comments

Comments
 (0)