Skip to content

Commit

Permalink
fix writeName8Raw ArrayIndexOutOfBoundsException error, for issue #2419
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 12, 2024
1 parent 5612e15 commit 1aef6dc
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ public final void writeName5Raw(long name) {
@Override
public final void writeName6Raw(long name) {
int off = this.off;
int minCapacity = off + 10 + indent;
int minCapacity = off + 11 + indent;
if (minCapacity >= this.chars.length) {
ensureCapacity(minCapacity);
}
Expand All @@ -1674,7 +1674,7 @@ public final void writeName6Raw(long name) {
@Override
public final void writeName7Raw(long name) {
int off = this.off;
int minCapacity = off + 10 + indent;
int minCapacity = off + 12 + indent;
if (minCapacity >= this.chars.length) {
ensureCapacity(minCapacity);
}
Expand All @@ -1698,7 +1698,7 @@ public final void writeName7Raw(long name) {
@Override
public final void writeName8Raw(long name) {
int off = this.off;
int minCapacity = off + 10 + indent;
int minCapacity = off + 13 + indent;
if (minCapacity >= this.chars.length) {
ensureCapacity(minCapacity);
}
Expand Down
64 changes: 64 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF16Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,68 @@ public void testLHex256BigEndian() {
);
assertEquals("abcd", new String(buf));
}

@Test
public void writeName8() {
JSONWriterUTF16 jsonWriter = new JSONWriterUTF16(JSONFactory.createWriteContext());

char[] name = "a123".toCharArray();
long nameValue = UNSAFE.getLong(name, ARRAY_CHAR_BASE_OFFSET);

{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName6Raw(nameValue);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName7Raw(nameValue);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName8Raw(nameValue);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName9Raw(nameValue, 1);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName10Raw(nameValue, 1);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName11Raw(nameValue, 1);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName12Raw(nameValue, 1);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName13Raw(nameValue, 1);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName14Raw(nameValue, 1);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName15Raw(nameValue, 1);
}
{
jsonWriter.chars = new char[0];
jsonWriter.off = 0;
jsonWriter.writeName16Raw(nameValue, 1);
}
}
}
62 changes: 62 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import static com.alibaba.fastjson2.JSONWriter.Feature.NullAsDefaultValue;
import static com.alibaba.fastjson2.JSONWriter.Feature.PrettyFormat;
import static com.alibaba.fastjson2.util.JDKUtils.ARRAY_CHAR_BASE_OFFSET;
import static com.alibaba.fastjson2.util.JDKUtils.UNSAFE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -669,4 +671,64 @@ public void writeStringLatin1Pretty() {
Object parse = JSON.parse(json);
assertEquals(str, parse);
}

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

char[] name = "a123".toCharArray();
long nameValue = UNSAFE.getLong(name, ARRAY_CHAR_BASE_OFFSET);

{
jsonWriter.bytes = new byte[0];
jsonWriter.writeName6Raw(nameValue);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.writeName7Raw(nameValue);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.writeName8Raw(nameValue);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.writeName9Raw(nameValue, 1);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.off = 0;
jsonWriter.writeName10Raw(nameValue, 1);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.off = 0;
jsonWriter.writeName11Raw(nameValue, 1);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.off = 0;
jsonWriter.writeName12Raw(nameValue, 1);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.off = 0;
jsonWriter.writeName13Raw(nameValue, 1);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.off = 0;
jsonWriter.writeName14Raw(nameValue, 1);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.off = 0;
jsonWriter.writeName15Raw(nameValue, 1);
}
{
jsonWriter.bytes = new byte[0];
jsonWriter.off = 0;
jsonWriter.writeName16Raw(nameValue, 1);
}
}
}

0 comments on commit 1aef6dc

Please sign in to comment.