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

View File

@ -15,6 +15,7 @@ package org.dromara.hutool.json;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.convert.impl.ArrayConverter;
import org.dromara.hutool.core.lang.Validator;
import org.dromara.hutool.core.lang.mutable.Mutable;
import org.dromara.hutool.core.lang.mutable.MutableEntry;
import org.dromara.hutool.core.lang.mutable.MutableObj;
@ -25,12 +26,7 @@ import org.dromara.hutool.json.writer.JSONWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;
import java.util.*;
import java.util.function.Predicate;
/**
@ -456,7 +452,15 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
}
this.rawList.add(index, InternalJSONUtil.wrap(element, this.config));
} else {
// 相对于5.x逻辑变更当index大于size则追加而不是补充null这样更加安全
// issue#3286, 如果用户指定的index太大容易造成Java heap space错误
if (!config.isIgnoreNullValue()) {
// issue#3286, 增加安全检查最多增加10倍
Validator.checkIndexLimit(index, this.size());
while (index != this.size()) {
// 非末尾则填充null
this.add(null);
}
}
this.add(element);
}

View File

@ -225,9 +225,9 @@ public class JSONArrayTest {
Assertions.assertEquals(1, jsonArray.size());
jsonArray = new JSONArray(JSONConfig.of().setIgnoreNullValue(false));
jsonArray.set(3, "test");
jsonArray.set(2, "test");
// 第三个位置插入值0~2都是null
Assertions.assertEquals(4, jsonArray.size());
Assertions.assertEquals(3, jsonArray.size());
}
// https://github.com/dromara/hutool/issues/1858