修复JSON解析栈溢出部分问题

This commit is contained in:
Looly
2022-11-29 23:11:36 +08:00
parent b0bfbd8ec3
commit d8283fedb0
7 changed files with 125 additions and 13 deletions

View File

@@ -1,3 +1,5 @@
package cn.hutool.json;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;

View File

@@ -0,0 +1,23 @@
package cn.hutool.json;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;
import org.junit.Test;
public class Issue2746Test {
@Test
public void parseObjTest() {
final String str = StrUtil.repeat("{", 1500) + StrUtil.repeat("}", 1500);
try{
JSONUtil.parseObj(str);
} catch (final JSONException e){
Assert.assertTrue(e.getMessage().startsWith("A JSONObject can not directly nest another JSONObject or JSONArray"));
}
}
@Test(expected = JSONException.class)
public void parseTest() {
final String str = StrUtil.repeat("[", 1500) + StrUtil.repeat("]", 1500);
JSONUtil.parseArray(str);
}
}

View File

@@ -0,0 +1,32 @@
package cn.hutool.json;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class Issue2749Test {
@Test
@Ignore
public void jsonObjectTest() {
final Map<String, Object> map = new HashMap<>(1, 1f);
Map<String, Object> node = map;
for (int i = 0; i < 1000; i++) {
//noinspection unchecked
node = (Map<String, Object>) node.computeIfAbsent("a", k -> new HashMap<String, Object>(1, 1f));
}
node.put("a", 1);
final String jsonStr = JSONUtil.toJsonStr(map);
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
final JSONObject jsonObject = new JSONObject(jsonStr);
Assert.assertNotNull(jsonObject);
// 栈溢出
//noinspection ResultOfMethodCallIgnored
jsonObject.toString();
}
}