mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
Fix issue 4169
This commit is contained in:
@@ -71,29 +71,28 @@ public class SplitIter extends ComputeIter<String> implements Serializable {
|
||||
return text.substring(offset);
|
||||
}
|
||||
|
||||
final int start = finder.start(offset);
|
||||
// 无分隔符,结束
|
||||
if (start < 0) {
|
||||
// 如果不再有分隔符,但是遗留了字符,则单独作为一个段
|
||||
if (offset <= text.length()) {
|
||||
final String result = text.substring(offset);
|
||||
if (false == ignoreEmpty || false == result.isEmpty()) {
|
||||
// 返回非空串
|
||||
offset = Integer.MAX_VALUE;
|
||||
return result;
|
||||
String result = null;
|
||||
int start;
|
||||
do {
|
||||
start = finder.start(offset);
|
||||
// 无分隔符,结束
|
||||
if (start < 0) {
|
||||
// 如果不再有分隔符,但是遗留了字符,则单独作为一个段
|
||||
if (offset <= text.length()) {
|
||||
result = text.substring(offset);
|
||||
if (!ignoreEmpty || !result.isEmpty()) {
|
||||
// 返回非空串
|
||||
offset = Integer.MAX_VALUE;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 找到新的分隔符位置
|
||||
final String result = text.substring(offset, start);
|
||||
offset = finder.end(start);
|
||||
|
||||
if (ignoreEmpty && result.isEmpty()) {
|
||||
// 发现空串且需要忽略时,跳过之
|
||||
return computeNext();
|
||||
}
|
||||
// 找到新的分隔符位置
|
||||
result = text.substring(offset, start);
|
||||
offset = finder.end(start);
|
||||
} while (ignoreEmpty && result.isEmpty()); // 空串则继续循环
|
||||
|
||||
count++;
|
||||
return result;
|
||||
|
||||
@@ -6,6 +6,8 @@ import cn.hutool.core.text.finder.PatternFinder;
|
||||
import cn.hutool.core.text.finder.StrFinder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -153,4 +155,18 @@ public class SplitIterTest {
|
||||
assertEquals(1, strings.size());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void issue4169Test() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 20000; i++) { // 1万次连续分隔符,模拟递归深度风险场景
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append("test");
|
||||
|
||||
SplitIter iter = new SplitIter(sb.toString(), new StrFinder(",",false), 0, true);
|
||||
List<String> result = iter.toList(false);
|
||||
|
||||
assertEquals(Collections.singletonList("test"), result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user