From 7f992d77bd444ff2b776872b85287d63508e4be0 Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 17 Mar 2023 07:43:36 +0100 Subject: [PATCH] fix unstacking issue with more than 400 elements in an array --- .../minidev/json/parser/JSONParserBase.java | 1 + .../net/minidev/json/test/TestOverflow.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java b/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java index 24d40c2a..87a9329a 100644 --- a/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java +++ b/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java @@ -620,6 +620,7 @@ protected T readObject(JsonReaderI mapper) throws ParseException, IOExcep // should loop skipping read step skipSpace(); if (c == '}') { + this.depth--; read(); /* unstack */ // return mapper.convert(current); diff --git a/json-smart/src/test/java/net/minidev/json/test/TestOverflow.java b/json-smart/src/test/java/net/minidev/json/test/TestOverflow.java index df99d1a8..07912f28 100644 --- a/json-smart/src/test/java/net/minidev/json/test/TestOverflow.java +++ b/json-smart/src/test/java/net/minidev/json/test/TestOverflow.java @@ -1,5 +1,6 @@ package net.minidev.json.test; +import net.minidev.json.JSONArray; import net.minidev.json.JSONValue; import net.minidev.json.parser.ParseException; @@ -29,4 +30,21 @@ public void stressTest() throws Exception { } assertTrue(false); } + + @Test + public void shouldNotFailParsingArraysWith400Elements() throws Exception { + int size = 400; + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i=0; i < size; i++) { + sb.append("{a:true}"); + if(i+1 < size) { + sb.append(","); + } + } + sb.append("]"); + String s = sb.toString(); + JSONArray array = (JSONArray) JSONValue.parseWithException(s); + assertEquals(array.size(), size); + } }