From ee9e7069b91010ec2856e9f84c9f1681d0071c1b Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 30 Oct 2017 08:10:06 -0400 Subject: [PATCH] fixes #372. Corrects behavior of unclosed arrays --- JSONArray.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 8775a1984..4864fd56e 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -107,7 +107,13 @@ public JSONArray(JSONTokener x) throws JSONException { if (x.nextClean() != '[') { throw x.syntaxError("A JSONArray text must start with '['"); } - if (x.nextClean() != ']') { + + char nextChar = x.nextClean(); + if (nextChar == 0) { + // array is unclosed. No ']' found, instead EOF + throw new JSONException(x.syntaxError("Expected a ',' or ']'")); + } + if (nextChar != ']') { x.back(); for (;;) { if (x.nextClean() == ',') { @@ -118,8 +124,16 @@ public JSONArray(JSONTokener x) throws JSONException { this.myArrayList.add(x.nextValue()); } switch (x.nextClean()) { + case 0: + // array is unclosed. No ']' found, instead EOF + throw new JSONException(x.syntaxError("Expected a ',' or ']'")); case ',': - if (x.nextClean() == ']') { + nextChar = x.nextClean(); + if (nextChar == 0) { + // array is unclosed. No ']' found, instead EOF + throw new JSONException(x.syntaxError("Expected a ',' or ']'")); + } + if (nextChar == ']') { return; } x.back();