修复SONArray的add()方法抛出OutOfMemory异常问题

This commit is contained in:
Looly 2023-08-30 22:10:03 +08:00
parent 45e9006a94
commit 9ba8f9ca5d
3 changed files with 11 additions and 1 deletions

View File

@ -25,7 +25,8 @@
* 【core 】 修复Ipv4Util.getEndIpLong 取反符号导致数据越界issue#I7U1OQ@Gitee
* 【http 】 修复302重定向时Location中的问号被转义问题issue#3265@Github
* 【core 】 修复CombinationAnnotationElement判断循环问题pr#3267@Github
* 【core 】 修复StrUtil#containsAny NPE问题问题pr#1063@Gitee
* 【core 】 修复StrUtil#containsAny NPE问题pr#1063@Gitee
* 【all 】 修复SONArray的add()方法抛出OutOfMemory异常问题issue#3286@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.21(2023-07-29)

View File

@ -2,6 +2,7 @@ package cn.hutool.core.collection;
import cn.hutool.core.comparator.PinyinComparator;
import cn.hutool.core.comparator.PropertyComparator;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Matcher;
import cn.hutool.core.util.ArrayUtil;
@ -431,6 +432,10 @@ public class ListUtil {
if (index < size) {
list.set(index, element);
} else {
// issue#3286, 增加安全检查最多增加2倍
if(index > (list.size() + 1) * 2) {
throw new UtilException("Index is too large:", index);
}
for (int i = size; i < index; i++) {
list.add(paddingElement);
}

View File

@ -457,6 +457,10 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
InternalJSONUtil.testValidity(element);
this.rawList.add(index, JSONUtil.wrap(element, this.config));
} else {
// issue#3286, 增加安全检查最多增加2倍
if(index > (this.size() + 1) * 2) {
throw new JSONException("Index is too large:", index);
}
while (index != this.size()) {
this.add(JSONNull.NULL);
}