Skip to content

Commit 9ba8f9c

Browse files
committed
修复SONArray的add()方法抛出OutOfMemory异常问题
1 parent 45e9006 commit 9ba8f9c

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

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

3031
-------------------------------------------------------------------------------------------------------------
3132
# 5.8.21(2023-07-29)

hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.hutool.core.comparator.PinyinComparator;
44
import cn.hutool.core.comparator.PropertyComparator;
5+
import cn.hutool.core.exceptions.UtilException;
56
import cn.hutool.core.lang.Assert;
67
import cn.hutool.core.lang.Matcher;
78
import cn.hutool.core.util.ArrayUtil;
@@ -431,6 +432,10 @@ public static <T> List<T> setOrPadding(List<T> list, int index, T element, T pad
431432
if (index < size) {
432433
list.set(index, element);
433434
} else {
435+
// issue#3286, 增加安全检查,最多增加2倍
436+
if(index > (list.size() + 1) * 2) {
437+
throw new UtilException("Index is too large:", index);
438+
}
434439
for (int i = size; i < index; i++) {
435440
list.add(paddingElement);
436441
}

hutool-json/src/main/java/cn/hutool/json/JSONArray.java

+4
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ public void add(int index, Object element) {
457457
InternalJSONUtil.testValidity(element);
458458
this.rawList.add(index, JSONUtil.wrap(element, this.config));
459459
} else {
460+
// issue#3286, 增加安全检查,最多增加2倍
461+
if(index > (this.size() + 1) * 2) {
462+
throw new JSONException("Index is too large:", index);
463+
}
460464
while (index != this.size()) {
461465
this.add(JSONNull.NULL);
462466
}

0 commit comments

Comments
 (0)