Merge pull request #3026 from wulangcode/v5-dev

fix:CollUtil.split优化切割列表参数判断,避免OOM
This commit is contained in:
Golden Looly 2023-03-29 21:58:15 +08:00 committed by GitHub
commit 1c6c15df07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 41 deletions

View File

@ -8,7 +8,7 @@
* 【core 】 SerializeUtil.deserialize增加白名单类避免RCE vulnerabilityissue#3021@Github
### 🐞Bug修复
* 【core 】 CollUtil.split优化切割列表参数判断避免OOM
-------------------------------------------------------------------------------------------------------------
# 5.8.16 (2023-03-26)

View File

@ -371,7 +371,7 @@ public class CollUtil {
result.addAll(coll1);
}
result.removeAll(coll2);
} catch (UnsupportedOperationException e){
} catch (UnsupportedOperationException e) {
// 针对 coll1 为只读集合的补偿
result = CollUtil.create(AbstractCollection.class);
result.addAll(coll1);
@ -1189,11 +1189,12 @@ public class CollUtil {
return result;
}
ArrayList<T> subList = new ArrayList<>(size);
final int initSize = Math.min(collection.size(), size);
List<T> subList = new ArrayList<>(initSize);
for (T t : collection) {
if (subList.size() >= size) {
result.add(subList);
subList = new ArrayList<>(size);
subList = new ArrayList<>(initSize);
}
subList.add(t);
}

View File

@ -255,6 +255,14 @@ public class CollUtilTest {
Assert.assertEquals(3, split.get(0).size());
}
@Test
public void splitTest2() {
final ArrayList<Integer> list = CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);
final List<List<Integer>> split = CollUtil.split(list, Integer.MAX_VALUE);
Assert.assertEquals(1, split.size());
Assert.assertEquals(9, split.get(0).size());
}
@Test
public void foreachTest() {
final HashMap<String, String> map = MapUtil.newHashMap();