alibaba / alibaba/fastjson2

[BUG]fastjson2.0.61版本,将字符串转成对象,如果数组型的字符串中含有\n回车,且字符串比较长,会出现数据越界异常。

Open
#7,685 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
fastjson2,将字符串转成对象,如果数组型的字符串中含有\n回车,且字符串比较长,会出现数据越界异常。

### 环境信息

- OS信息: windows和linux
- JDK信息:jdk-21.0.7
- 版本信息:Fastjson2 2.0.61,其他版本没有这个问题,比如Fastjson2 2.0.60,Fastjson2 2.0.53,Fastjson2 2.0.54

### 重现步骤
1. fastjson2,将字符串转成对象,如果数组型的字符串中含有\n回车,且字符串比较长,会出现数据越界异常。
2. 可以执行运行如下代码
```java
String str= "{\"Test\":[{\"message\":\"中\\n\"},{\"message\":\"s Howard), dedicated to dinosaur conservation, was invited by Benjamin Lockwood. Benjamin and his subordinate Eli Mills (played by Rafi Spo) hope that she and relevant technicians will go to Isla Nublar to save the dinosaurs there and relocate them to a new habitat. Claire, who is infected, manages to bring Owen (played by Chris Pratt) along to go, while Owen hopes to rescue the velociraptor Blue that he has personally tamed. When they set foot on this isolated island, they found that things were not that simple.\\nVolcanoes are restless and ready to erupt, while ugly conspiracies are quietly brewing amidst chaos.\"}]}";
JSON.parseObject(str, Object.class);
```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*

### 相关日志输出
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 516 out of bounds for char[512]
at java.base/java.lang.System.arraycopy(Native Method)
at com.alibaba.fastjson2.JSONReaderUTF16.readString(JSONReaderUTF16.java:3259)
at com.alibaba.fastjson2.JSONReader.readObject(JSONReader.java:3777)
at com.alibaba.fastjson2.JSONReader.readArray(JSONReader.java:4118)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:209)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:992)
at Test.main(Test.java:2222)

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*

Contributor guide

Open the contributing guide

Research direction

Start with the supplied reproducer and the stack trace at JSONReaderUTF16.java:3259, then read the surrounding string-reading path through JSONReader.java and the array handling shown in the trace. Confirm the failure on Java 21 with Fastjson2 2.0.61 and verify that the same input parses successfully without the ArrayIndexOutOfBoundsException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.