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] jdk17 JSONB.toBytes 数组越界 #1812

Closed
SmileTower opened this issue Aug 30, 2023 · 5 comments
Closed

[BUG] jdk17 JSONB.toBytes 数组越界 #1812

SmileTower opened this issue Aug 30, 2023 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@SmileTower
Copy link

SmileTower commented Aug 30, 2023

问题描述

简要描述您碰到的问题。

环境信息

请填写以下信息:

  • OS信息: [e.g.:Win10]
  • JDK信息: [e.g.:Oracle Jdk 17.0.8]
  • 版本信息:[e.g.:Fastjson2 2.0.39]

重现步骤

@Data
public class Driver3 implements Serializable {
    private List<String> names;
}

@Test
  public void tes10() {
      List<Map<String, List<String>>> list = Lists.newArrayList();
      for (int i = 0; i < 10; i++) {
          Map<String, List<String>> map = Maps.newHashMap();
          map.put("names", ListUtil.of(RandomUtil.randomString(10000)));
          list.add(map);
      }
      List<Driver3> s1 = JSON.parseArray(JSON.toJSONString(list), Driver3.class);
      JSONB.toBytes(s1);
  }

java 8 正常,java 11、17 都报错
只要最简单的嵌套, JSONB.toBytes 超过一定数量就报错,只要改变循环次数或者随机字符串长度即会报错,应该跟哪块内存有关

期待的正确结果

相关日志输出

java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 209962 out of bounds for byte[209952]

	at java.base/java.lang.System.arraycopy(Native Method)
	at com.alibaba.fastjson2.JSONWriterJSONB.writeString(JSONWriterJSONB.java:695)
	at com.alibaba.fastjson2.writer.OWG_1_47_DriverEntity2.writeJSONB(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplList.writeJSONB(ObjectWriterImplList.java:230)
	at com.alibaba.fastjson2.JSONB.toBytes(JSONB.java:1104)

附加信息

@SmileTower SmileTower added the bug Something isn't working label Aug 30, 2023
@wenshao
Copy link
Member

wenshao commented Aug 30, 2023

@wenshao wenshao added this to the 2.0.40 milestone Aug 30, 2023
@wenshao wenshao added the fixed label Aug 30, 2023
@SmileTower
Copy link
Author

SmileTower commented Aug 31, 2023

验证 jdk17 还是异常,jdk8 STRING_VALUE = null , 不走下面的copy代码
1693450406559
1693450485570

测试字符串长度(12276) + off(9) + 3 >= 8192 x 1.5倍,就会越界
com.alibaba.fastjson2.JSONWriterJSONB#writeInt32(int)
off++ , off += 5,所有 off 最多 加6 ,
ensureCapacity(off + strlen + 6) 即可

@wenshao issue1812.json 存在敏感信息,希望删除,上面的修正后的测试方法更简单

@wenshao
Copy link
Member

wenshao commented Aug 31, 2023

已经删除内容,并且修复问题,请更新快照帮忙再验证下

@SmileTower
Copy link
Author

验证OK,感谢 温少

@wenshao
Copy link
Member

wenshao commented Sep 3, 2023

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

@wenshao wenshao closed this as completed Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants