This commit is contained in:
Looly
2023-09-01 23:41:00 +08:00
parent 56abd26590
commit 645ee387d3
6 changed files with 45 additions and 26 deletions

View File

@@ -2,9 +2,9 @@ package org.dromara.hutool.core.array;
import org.dromara.hutool.core.collection.iter.ArrayIter;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.func.Wrapper;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Validator;
import org.dromara.hutool.core.reflect.ClassUtil;
import org.dromara.hutool.core.util.ObjUtil;
@@ -294,10 +294,8 @@ public class ArrayWrapper<A, E> implements Wrapper<A>, Iterable<E> {
if (index < this.length) {
Array.set(array, index, value);
} else {
// issue#3286, 增加安全检查,最多增加2
if(index > (length + 1) * 2) {
throw new HutoolException("Index is too large:", index);
}
// issue#3286, 增加安全检查,最多增加10
Validator.checkIndexLimit(index, this.length);
for (int i = length; i < index; i++) {
append(paddingElement);

View File

@@ -20,8 +20,8 @@ import org.dromara.hutool.core.collection.partition.RandomAccessAvgPartition;
import org.dromara.hutool.core.collection.partition.RandomAccessPartition;
import org.dromara.hutool.core.comparator.PinyinComparator;
import org.dromara.hutool.core.comparator.PropertyComparator;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Validator;
import org.dromara.hutool.core.lang.page.PageInfo;
import org.dromara.hutool.core.util.ObjUtil;
@@ -477,10 +477,8 @@ public class ListUtil {
if (index < size) {
list.set(index, element);
} else {
// issue#3286, 增加安全检查,最多增加2
if(index > (list.size() + 1) * 2) {
throw new HutoolException("Index is too large:", index);
}
// issue#3286, 增加安全检查,最多增加10
Validator.checkIndexLimit(index, size);
for (int i = size; i < index; i++) {
list.add(paddingElement);
}

View File

@@ -12,17 +12,17 @@
package org.dromara.hutool.core.lang;
import org.dromara.hutool.core.data.CreditCodeUtil;
import org.dromara.hutool.core.data.IdcardUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.exception.ValidateException;
import org.dromara.hutool.core.regex.PatternPool;
import org.dromara.hutool.core.regex.RegexPool;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.data.CreditCodeUtil;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.regex.PatternPool;
import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.regex.RegexPool;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.data.IdcardUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.util.ObjUtil;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
@@ -1239,4 +1239,23 @@ public class Validator {
throw new ValidateException(errorMsg);
}
}
/**
* 检查给定的index是否超出长度限制默认检查超出倍数10倍此方法主要用于内部检查包括
* <ul>
* <li>数组调用setOrPadding时最多允许padding的长度</li>
* <li>List调用setOrPadding时最多允许padding的长度</li>
* <li>JSONArray调用setOrPadding时最多允许padding的长度</li>
* </ul>
*
* @param index 索引
* @param size 数组、列表长度
* @since 6.0.0
*/
public static void checkIndexLimit(final int index, final int size) {
// issue#3286, 增加安全检查最多增加10倍
if (index > (size + 1) * 10) {
throw new ValidateException("Index [{}] is too large for size: [{}]", index, size);
}
}
}

View File

@@ -41,7 +41,7 @@ public class ClassPathResourceTest {
// 读取classpath根目录测试
final ClassPathResource resource = new ClassPathResource("/");
final String content = resource.readUtf8Str();
Assertions.assertTrue(StrUtil.isEmpty(content));
Assertions.assertNotNull(content);
}
@Test