mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复JSON解析栈溢出部分问题
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.json.JSON;
|
||||
import cn.hutool.json.JSONConfig;
|
||||
import cn.hutool.json.JSONObject;
|
23
hutool-json/src/test/java/cn/hutool/json/Issue2746Test.java
Executable file
23
hutool-json/src/test/java/cn/hutool/json/Issue2746Test.java
Executable 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);
|
||||
}
|
||||
}
|
32
hutool-json/src/test/java/cn/hutool/json/Issue2749Test.java
Executable file
32
hutool-json/src/test/java/cn/hutool/json/Issue2749Test.java
Executable 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user