Skip to content

Commit

Permalink
Stack Overflow fix on malformed JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
coheigea authored and dkulp committed Sep 21, 2022
1 parent a5d2223 commit 395f862
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/org/codehaus/jettison/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ public char nextClean() throws JSONException {
}
break;
default:
if (!more()) {
throw syntaxError("The JSON text is malformed");
}
back();
return '/';
}
Expand Down
57 changes: 56 additions & 1 deletion src/test/java/org/codehaus/jettison/json/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,60 @@ public void testForwardSlashEscapingModifiedfBySetter() throws Exception {
assertEquals(obj.toString(), "{\"key\":\"http://example.com/foo\"}");
obj.setEscapeForwardSlashAlways(true);
assertEquals(obj.toString(), "{\"key\":\"http:\\/\\/example.com\\/foo\"}");
}
}

public void testMalformedObject() throws Exception {
try {
new JSONObject("{/");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}

public void testMalformedObject2() throws Exception {
try {
new JSONObject("{x");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}

public void testMalformedObject3() throws Exception {
try {
new JSONObject("{/x");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}

public void testMalformedObject4() throws Exception {
try {
new JSONObject("{/*");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}

public void testMalformedObject5() throws Exception {
try {
new JSONObject("{//");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}

public void testMalformedArray() throws Exception {
try {
new JSONObject("{[/");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}

}

0 comments on commit 395f862

Please sign in to comment.