Skip to content

Commit

Permalink
BugFix: 修复JSONWriterUTF8.writeStringEscaped申请数组长度不够问题
Browse files Browse the repository at this point in the history
int minCapacity = off + values.length * 4 + 2;
修改为:
int minCapacity = off + values.length * 6 + 2;
  • Loading branch information
cychen1981 committed Dec 26, 2024
1 parent 2947043 commit b5b09cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ public final void writeString(final char[] chars, int stroff, int strlen) {
}

protected final void writeStringEscaped(byte[] values) {
int minCapacity = off + values.length * 4 + 2;
int minCapacity = off + values.length * 6 + 2;
if (minCapacity >= this.bytes.length) {
ensureCapacity(minCapacity);
}
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF8Test.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alibaba.fastjson2;

import com.alibaba.fastjson2.JSONWriter.Context;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -672,6 +673,20 @@ public void writeStringLatin1Pretty() {
assertEquals(str, parse);
}

@Test
public void writeStringEscaped() {
byte[] bytes = new byte[1024 * 128];
Arrays.fill(bytes, (byte) 1);
Context context = JSONFactory.createWriteContext(JSONWriter.Feature.PrettyFormat);
try (JSONWriterUTF8 jsonWriter = new JSONWriterUTF8(context)) {
jsonWriter.writeStringEscaped(bytes);
String json = jsonWriter.toString();
String str = new String(bytes, 0, bytes.length, StandardCharsets.ISO_8859_1);
Object parse = JSON.parse(json);
assertEquals(str, parse);
}
}

@Test
public void writeName8() {
JSONWriterUTF8 jsonWriter = new JSONWriterUTF8(JSONFactory.createWriteContext());
Expand Down

0 comments on commit b5b09cd

Please sign in to comment.