Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]CSVWriter 写入CSV数据超过 65536 个字节时报错 [漏网之鱼] #2988

Open
CodePlayer opened this issue Sep 27, 2024 · 0 comments
Labels
bug Something isn't working
Milestone

Comments

@CodePlayer
Copy link
Contributor

CodePlayer commented Sep 27, 2024

之前是在 #2848 的评论中补充,不过该 ISSUE 已经被关闭,也无法重新开启,所以在此再次反馈,以免被忽略。

还有几个方法存在类似的 bug,此处不再赘述,可以直接参考 #2848 的补充评论。

@wenshao 用新版本测试了下,writeBigDecimal 的问题已经解决了,不过又发现了一个类似的 bug。

OutputStreamWriter out = new OutputStreamWriter(new ByteArrayOutputStream(), StandardCharsets.UTF_8);
try (CSVWriter writer = CSVWriter.of(out)) {
	writer.writeValue("1".repeat(65534));
	writer.writeComma();
	writer.writeString("123"); // java.lang.StringIndexOutOfBoundsException: offset 65535, count 3, length 65536
}

if (escapeCount == 0) {
str.getChars(0, str.length(), chars, off);
off += str.length();
return;
}
if (off + 2 + str.length() + escapeCount >= chars.length) {
flush();
}

定位了下,如上所示,是 CSVWriterUTF16142 行代码处没有 预先检测数组容量 所致。

错误堆栈信息如下:

java.lang.StringIndexOutOfBoundsException: offset 65535, count 3, length 65536

	at java.base/java.lang.String.checkBoundsOffCount(String.java:4593)
	at java.base/java.lang.String.getChars(String.java:1681)
	at com.alibaba.fastjson2.support.csv.CSVWriterUTF16.writeString(CSVWriterUTF16.java:142)
	at test.FastjsonTest.test(FastjsonTest.java:56)

发现这些 bug 有些随机,必须要恰好构造出符合特定条件的数据才能出现。

Originally posted by @CodePlayer in #2848 (comment)

@CodePlayer CodePlayer changed the title CSVWriter 写入CSV数据超过 65536 个字节时报错 [漏网之鱼] [BUG]CSVWriter 写入CSV数据超过 65536 个字节时报错 [漏网之鱼] Sep 27, 2024
@wenshao wenshao added this to the 2.0.54 milestone Sep 27, 2024
@wenshao wenshao added the bug Something isn't working label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants