Skip to content

Commit

Permalink
Don't skip past \0 when parsing JSON objects.
Browse files Browse the repository at this point in the history
A better solution might be to use -1 instead 0 to represent EOF everywhere,
which of course means changing `char` variables to `int`. The solution here is
enough to solve the immediate problem, though.

Fixes #758.
  • Loading branch information
eamonnmcmanus committed Aug 1, 2023
1 parent 402db6a commit c8a9e15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ public JSONObject(JSONTokener x) throws JSONException {
switch (x.nextClean()) {
case ';':
case ',':
if (x.nextClean() == '}') {
c = x.nextClean();
if (c == 0) {
throw x.syntaxError("A JSONObject text must end with '}'");
}
if (c == '}') {
return;
}
x.back();
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,15 @@ public void jsonObjectParsingErrors() {
"Expected a ',' or '}' at 15 [character 16 line 1]",
e.getMessage());
}
try {
// \0 after ,
String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
assertNull("Expected an exception",new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"A JSONObject text must end with '}' at 15 [character 16 line 1]",
e.getMessage());
}
try {
// append to wrong key
String str = "{\"myKey\":true, \"myOtherKey\":false}";
Expand Down

0 comments on commit c8a9e15

Please sign in to comment.