Skip to content

Commit

Permalink
fix readComment, for issue #3260
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 11, 2025
1 parent 2c76718 commit 949e867
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ static JSONPathFilter.Operator parseOperator(JSONReader jsonReader) {
case '=':
jsonReader.next();
if (jsonReader.ch == '~') {
jsonReader.next();
jsonReader.nextWithoutComment();
operator = JSONPathFilter.Operator.REG_MATCH;
} else if (jsonReader.ch == '=') {
jsonReader.next();
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ public final int getOffset() {

public abstract void next();

public void nextWithoutComment() {
next();
}

public abstract long readValueHashCode();

public long readTypeHashCode() {
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,18 @@ public final void next() {
}
}

@Override
public final void nextWithoutComment() {
int offset = this.offset;
final char[] chars = this.chars;
char ch = offset >= end ? EOI : chars[offset++];
while (ch == '\0' || (ch <= ' ' && ((1L << ch) & SPACE) != 0)) {
ch = offset == end ? EOI : chars[offset++];
}
this.offset = offset;
this.ch = ch;
}

@Override
public final long readFieldNameHashCodeUnquote() {
this.nameEscape = false;
Expand Down Expand Up @@ -3420,7 +3432,7 @@ public final void skipComment() {
} else if (ch == '/') {
multi = false;
} else {
return;
throw new JSONException(info("parse comment error"));
}

ch = chars[offset++];
Expand Down
20 changes: 19 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,23 @@ public void next() {
}
}

public final void nextWithoutComment() {
final byte[] bytes = this.bytes;
int offset = this.offset;
int ch = offset >= end ? EOI : bytes[offset++];
while (ch == '\0' || (ch <= ' ' && ((1L << ch) & SPACE) != 0)) {
ch = offset == end ? EOI : bytes[offset++];
}

if (ch < 0) {
char_utf8(ch, offset);
return;
}

this.offset = offset;
this.ch = (char) ch;
}

@Override
public long readFieldNameHashCodeUnquote() {
this.nameEscape = false;
Expand Down Expand Up @@ -4734,14 +4751,15 @@ public final void skipComment() {

final byte[] bytes = this.bytes;
byte ch = bytes[offset++];
byte start = ch;

boolean multi;
if (ch == '*') {
multi = true;
} else if (ch == '/') {
multi = false;
} else {
return;
throw new JSONException(info("parse comment error"));
}

ch = bytes[offset++];
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/java/com/alibaba/fastjson2/JSONReaderTest1.java
Original file line number Diff line number Diff line change
Expand Up @@ -943,20 +943,20 @@ public void test_readLocalDateTimeX() {
@Test
public void test_readPattern() {
for (JSONReader jsonReader : TestUtils.createJSONReaders4("1 /abc/")) {
jsonReader.next();
jsonReader.nextWithoutComment();
assertEquals("abc", jsonReader.readPattern());
assertEquals(JSONReader.EOI, jsonReader.ch);
assertFalse(jsonReader.comma);
}
for (JSONReader jsonReader : TestUtils.createJSONReaders4("1 /abc/ , ")) {
jsonReader.next();
jsonReader.nextWithoutComment();
assertEquals("abc", jsonReader.readPattern());
assertEquals(JSONReader.EOI, jsonReader.ch);
assertTrue(jsonReader.comma);
}

for (JSONReader jsonReader : TestUtils.createJSONReaders4("1 /abc/ , 1")) {
jsonReader.next();
jsonReader.nextWithoutComment();
assertEquals("abc", jsonReader.readPattern());
assertEquals('1', jsonReader.ch);
assertTrue(jsonReader.comma);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.alibaba.fastjson2.issues_3200;

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

import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class Issue3260 {
@Test
public void test() {
String str = "{\"dns\":[\"<>\"/'\"],\"operation\":\"init\"}";
assertThrows(
JSONException.class,
() -> JSON.parseObject(str));
assertThrows(
JSONException.class,
() -> JSON.parseObject(str.toCharArray()));
assertThrows(
JSONException.class,
() -> JSON.parseObject(str.getBytes(StandardCharsets.UTF_8)));
}
}

0 comments on commit 949e867

Please sign in to comment.