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

@@ -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