Skip to content
New issue

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

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

Already on GitHub? # to your account

[BUG]java.lang.ArrayIndexOutOfBoundsException: Index 12288 out of bounds for length 12288 #3209

Closed
yourpleasure opened this issue Dec 16, 2024 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@yourpleasure
Copy link

问题描述

序列化过程中跑出数组越界问题

环境信息

  • OS信息: Debian 5.4.210.bsk.6 128Core 3.00GHz 512 GB]
  • JDK信息: openjdk 11.0.19
  • 版本信息:fastjson 2.0.0.53

重现步骤

使用jdk8执行无问题,使用jdk11版本执行对特定的数据会出错,具体引起错误的记录还没有找到

期待的正确结果

正常系列化为byte数据,无异常

相关日志输出

java.lang.ArrayIndexOutOfBoundsException: Index 12288 out of bounds for length 12288
at com.alibaba.fastjson2.JSONWriterUTF8.writeStringEscaped(JSONWriterUTF8.java:931)
at com.alibaba.fastjson2.JSONWriterUTF8.writeStringLatin1(JSONWriterUTF8.java:515)
at com.alibaba.fastjson2.JSONWriterUTF8JDK9.writeString(JSONWriterUTF8JDK9.java:22)
at com.alibaba.fastjson2.writer.ObjectWriterImplMap.write(ObjectWriterImplMap.java:494)
at com.alibaba.fastjson2.JSON.toJSONBytes(JSON.java:3238)

@yourpleasure yourpleasure added the bug Something isn't working label Dec 16, 2024
@wenshao wenshao added this to the 2.0.54 milestone Dec 16, 2024
@tanghaoth90
Copy link

Hi @wenshao

在JSONWriterUTF8.java里面第880行

    protected final void writeStringEscaped(byte[] values) {
        int minCapacity = off + values.length * 4 + 2;

当字符是ASCII 0~7, 11, 14, 15, ... 的时候,根据values.length * 4算出来的minCapacity应该是不够大的。
每个字节扩展为 \uxxxx 一共是6个字符。所以values.length * 6才是合理的。

可能需要加上类似第778行这样的代码

        if (escapeNoneAscii || browserSecure) {
            minCapacity += chars.length * 3;
        }

@macroguo-ghy
Copy link

macroguo-ghy commented Dec 19, 2024

除了等新版本升级之外,还有解决方案吗? 我也遇到了这个问题

@cychen1981
Copy link
Collaborator

已在issue3209中修复该问题

@tanghaoth90
Copy link

关联 pull request #3321

@wenshao wenshao added the fixed label Jan 1, 2025
@wenshao
Copy link
Member

wenshao commented Jan 12, 2025

https://github.com/alibaba/fastjson2/releases/tag/2.0.54
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jan 12, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

5 participants