Skip to content

Commit 395f862

Browse files
coheigeadkulp
authored andcommitted
Stack Overflow fix on malformed JSON
1 parent a5d2223 commit 395f862

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/main/java/org/codehaus/jettison/json/JSONTokener.java

+3
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ public char nextClean() throws JSONException {
197197
}
198198
break;
199199
default:
200+
if (!more()) {
201+
throw syntaxError("The JSON text is malformed");
202+
}
200203
back();
201204
return '/';
202205
}

src/test/java/org/codehaus/jettison/json/JSONObjectTest.java

+56-1
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,60 @@ public void testForwardSlashEscapingModifiedfBySetter() throws Exception {
9292
assertEquals(obj.toString(), "{\"key\":\"http://example.com/foo\"}");
9393
obj.setEscapeForwardSlashAlways(true);
9494
assertEquals(obj.toString(), "{\"key\":\"http:\\/\\/example.com\\/foo\"}");
95-
}
95+
}
96+
97+
public void testMalformedObject() throws Exception {
98+
try {
99+
new JSONObject("{/");
100+
fail("Failure expected on malformed JSON");
101+
} catch (JSONException ex) {
102+
// expected
103+
}
104+
}
105+
106+
public void testMalformedObject2() throws Exception {
107+
try {
108+
new JSONObject("{x");
109+
fail("Failure expected on malformed JSON");
110+
} catch (JSONException ex) {
111+
// expected
112+
}
113+
}
114+
115+
public void testMalformedObject3() throws Exception {
116+
try {
117+
new JSONObject("{/x");
118+
fail("Failure expected on malformed JSON");
119+
} catch (JSONException ex) {
120+
// expected
121+
}
122+
}
123+
124+
public void testMalformedObject4() throws Exception {
125+
try {
126+
new JSONObject("{/*");
127+
fail("Failure expected on malformed JSON");
128+
} catch (JSONException ex) {
129+
// expected
130+
}
131+
}
132+
133+
public void testMalformedObject5() throws Exception {
134+
try {
135+
new JSONObject("{//");
136+
fail("Failure expected on malformed JSON");
137+
} catch (JSONException ex) {
138+
// expected
139+
}
140+
}
141+
142+
public void testMalformedArray() throws Exception {
143+
try {
144+
new JSONObject("{[/");
145+
fail("Failure expected on malformed JSON");
146+
} catch (JSONException ex) {
147+
// expected
148+
}
149+
}
150+
96151
}

0 commit comments

Comments
 (0)