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

fix writeName8Raw ArrayIndexOutOfBoundsException error, for issue #24… #2434

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading