From a99adf609141beda967afe657389c03d5b092e98 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 17 May 2024 09:32:45 +0800 Subject: [PATCH] =?UTF-8?q?ListUtil.setOrPadding=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=87=8D=E8=BD=BD=EF=BC=8C=E5=8F=AF=E9=80=89=E9=99=90=E5=88=B6?= =?UTF-8?q?index=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- .../cn/hutool/core/collection/ListUtil.java | 30 ++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acb76b370..9990c45e2 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.28(2024-05-13) +# 5.8.28(2024-05-17) ### 🐣新特性 * 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee) @@ -21,6 +21,7 @@ * 【http 】 HttpRequest#get不再尝试File路径(issue#I9O6DA@Gitee) * 【core 】 增加IdConstants,提高Snowflake初始化性能(issue#3581@Github) * 【core 】 优化 CharSequenceUtil工具类 startWithAny()、startWithAnyIgnoreCase() 参数命名错误问题(pr#1219@Gitee) +* 【core 】 ListUtil.setOrPadding增加重载,可选限制index大小(issue#3586@Github) ### 🐞Bug修复 * 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java index eb21e7fd2..88debbf3e 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java @@ -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.ValidateException; import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Matcher; import cn.hutool.core.lang.Validator; @@ -416,7 +417,8 @@ public class ListUtil { } /** - * 在指定位置设置元素。当index小于List的长度时,替换指定位置的值,否则追加{@code paddingElement}直到到达index后,设置值 + * 在指定位置设置元素。当index小于List的长度时,替换指定位置的值,否则追加{@code paddingElement}直到到达index后,设置值
+ * 注意:为避免OOM问题,此方法限制index的最大值为{@code (list.size() + 1) * 10} * * @param 元素类型 * @param list List列表 @@ -424,16 +426,36 @@ public class ListUtil { * @param element 新元素 * @param paddingElement 填充的值 * @return 原List - * @since 5。8.4 + * @since 5.8.4 */ public static List setOrPadding(List list, int index, T element, T paddingElement) { + return setOrPadding(list, index, element, paddingElement, (list.size() + 1) * 10); + } + + /** + * 在指定位置设置元素。当index小于List的长度时,替换指定位置的值,否则追加{@code paddingElement}直到到达index后,设置值 + * + * @param 元素类型 + * @param list List列表 + * @param index 位置 + * @param element 新元素 + * @param paddingElement 填充的值 + * @param indexLimit 最大索引限制 + * @return 原List + * @since 5.8.28 + */ + public static List setOrPadding(List list, int index, T element, T paddingElement, int indexLimit) { Assert.notNull(list, "List must be not null !"); final int size = list.size(); if (index < size) { list.set(index, element); } else { - // issue#3286, 增加安全检查,最多增加10倍 - Validator.checkIndexLimit(index, list.size()); + if(indexLimit > 0){ + // issue#3286, 增加安全检查 + if (index > indexLimit) { + throw new ValidateException("Index [{}] is too large for limit: [{}]", index, indexLimit); + } + } for (int i = size; i < index; i++) { list.add(paddingElement); }