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 Issue2592 #3215

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class JSONReaderUTF16
}
ch = chars[this.offset];
}

if (ch != '{') {
ch = EOI;
return;
}
this.offset++;
}

Expand Down Expand Up @@ -138,6 +143,11 @@ class JSONReaderUTF16
}
ch = chars[this.offset];
}

if (ch != '{') {
ch = EOI;
return;
}
this.offset++;
}

Expand Down Expand Up @@ -184,6 +194,11 @@ class JSONReaderUTF16
}
ch = chars[this.offset];
}

if (ch != '{') {
ch = EOI;
return;
}
this.offset++;
}

Expand Down Expand Up @@ -222,6 +237,10 @@ class JSONReaderUTF16
}
ch = chars[this.offset];
}
if (ch != '{') {
ch = EOI;
return;
}
this.offset++;
}

Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ public void next() {
return;
}

if (ch != '{') {
ch = EOI;
return;
}

this.offset = offset;
this.ch = (char) ch;
if (ch == '/') {
Expand Down
120 changes: 120 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues_2500/Issue2592.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.alibaba.fastjson2.issues_2500;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class Issue2592 {
String str = "\"name\":\"Huang\",\"minVersion\":\"1.1\",\"maxVersion\":\"1.2\"}";

@Test
void test() {
Exception error = null;
try {
JSONObject.parseObject(str, TestBean.class);
} catch (JSONException e) {
error = e;
}
assertNotNull(error);
assertTrue(error.getMessage().contains("illegal fieldName input"));
}

@Test
void testUTF8() {
byte[] utf8Bytes = str.getBytes(StandardCharsets.UTF_8);
Exception error = null;
try {
JSON.parseObject(utf8Bytes, TestBean.class);
} catch (JSONException e) {
error = e;
}
assertNotNull(error);
assertTrue(error.getMessage().contains("illegal fieldName input"));
}

@Test
void testUTF8Stream() {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes());
Exception error = null;
try {
JSON.parseObject(byteArrayInputStream, StandardCharsets.UTF_8);
} catch (JSONException e) {
error = e;
}
assertNotNull(error);
}

@Test
void testUTF8Stream2() {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes());
InputStreamReader inputStreamReader = new InputStreamReader(byteArrayInputStream);
JSONReader jsonReader = JSONReader.of(inputStreamReader);
Exception error = null;
try {
jsonReader.read(TestBean.class);
} catch (JSONException e) {
error = e;
}
assertNotNull(error);
assertTrue(error.getMessage().contains("illegal fieldName input"));
}

@Test
void testUTF16() {
String str1 = "\"name\": \"张三\", \"minVersion\": 30, \"maxVersion\": \"北京\" }";
byte[] utf16Bytes = str1.getBytes(StandardCharsets.UTF_16);
JSONReader jsonReader = JSONReader.of(utf16Bytes, 0, utf16Bytes.length, StandardCharsets.UTF_16);
Exception error = null;
try {
jsonReader.read(TestBean.class);
} catch (JSONException e) {
error = e;
}
assertNotNull(error);
assertTrue(error.getMessage().contains("illegal fieldName input"));
}
@Test
void testValid() {
boolean isValid = JSON.isValid(str);
assertFalse(isValid);
}

public static class TestBean {
String name;
String minVersion;
String maxVersion;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMinVersion() {
return minVersion;
}

public void setMinVersion(String minVersion) {
this.minVersion = minVersion;
}

public String getMaxVersion() {
return maxVersion;
}

public void setMaxVersion(String maxVersion) {
this.maxVersion = maxVersion;
}
}
}
Loading