Skip to content

Commit

Permalink
bug fix for writeBinary ensureCapacity, fix issue #537
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 8, 2022
1 parent 7baea90 commit 3612a52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ public void writeBinary(byte[] bytes) {
return;
}

ensureCapacity(off + 5 + bytes.length);
ensureCapacity(off + 6 + bytes.length);
this.bytes[off++] = BC_BINARY;
writeInt32(bytes.length);

Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue537.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.alibaba.fastjson2.issues;

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

import java.lang.reflect.Field;

public class Issue537 {
@Test
public void test() throws Exception {
JSONWriter writer = JSONWriter.ofJSONB();
Field field = writer.getClass().getDeclaredField("bytes");
field.setAccessible(true);
field.set(writer, new byte[1024 * 1024 + 5]);

byte[] bytes = new byte[1024 * 1024];
writer.writeBinary(bytes);
}
}

0 comments on commit 3612a52

Please sign in to comment.