From 1e3f37be98f3bcb071be900fb0a7292da52fbb37 Mon Sep 17 00:00:00 2001 From: rikkarth Date: Sat, 27 Apr 2024 22:37:21 +0100 Subject: [PATCH] feat(#877): add additional validation, test case --- src/main/java/org/json/JSONArray.java | 13 ++++++++++--- .../org/json/junit/JSONParserConfigurationTest.java | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index bcd95f9e3..e7d5f52fa 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -121,6 +121,10 @@ private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfi throwErrorIfEoF(x); + if(strictMode && cursor == ']'){ + throw x.syntaxError(getInvalidCharErrorMsg(cursor)); + } + if (cursor == ']') { break; } @@ -135,7 +139,7 @@ private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfi boolean isNotEoF = !x.end(); if (isNotEoF && x.getArrayLevel() == 0) { - throw x.syntaxError(String.format("invalid character '%s' found after end of array", cursor)); + throw x.syntaxError(getInvalidCharErrorMsg(cursor)); } x.back(); @@ -147,7 +151,7 @@ private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfi boolean quoteIsNotNextToValidChar = x.getPreviousChar() != ',' && x.getPreviousChar() != '['; if (strictMode && currentCharIsQuote && quoteIsNotNextToValidChar) { - throw x.syntaxError(String.format("invalid character '%s' found after end of array", cursor)); + throw x.syntaxError(getInvalidCharErrorMsg(cursor)); } this.myArrayList.add(x.nextValue(jsonParserConfiguration)); @@ -1954,6 +1958,7 @@ private void addAll(Object array, boolean wrap) throws JSONException { private void addAll(Object array, boolean wrap, int recursionDepth) { addAll(array, wrap, recursionDepth, new JSONParserConfiguration()); } + /** * Add an array's elements to the JSONArray. *` @@ -2000,7 +2005,6 @@ private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserCo "JSONArray initial value should be a string or collection or array."); } } - /** * Create a new JSONException in a common format for incorrect conversions. * @param idx index of the item @@ -2029,4 +2033,7 @@ private static JSONException wrongValueFormatException( , cause); } + private static String getInvalidCharErrorMsg(char cursor) { + return String.format("invalid character '%s' found after end of array", cursor); + } } diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java index 69045421d..2cea15f2d 100644 --- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -287,6 +287,7 @@ public void verifyMaxDepthThenDuplicateKey() { private List getNonCompliantJSONList() { return Arrays.asList( "[1],", + "[1,]", "[[1]\"sa\",[2]]a", "[1],\"dsa\": \"test\"", "[[a]]",