Skip to content

Commit 1785374

Browse files
committed
check for incomplete read
1 parent 0dfc3ea commit 1785374

File tree

7 files changed

+54
-11
lines changed

7 files changed

+54
-11
lines changed

demo/src/test/java/com/jsoniter/demo/ModelTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static void main(String[] args) throws IOException, RunnerException {
5151
"-i", "5",
5252
"-wi", "5",
5353
"-f", "1",
54+
// "-jvmArgsAppend", "-server -XX:+DoEscapeAnalysis",
5455
});
5556
}
5657

@@ -89,7 +90,7 @@ public void jsoniter_easy_mode(Blackhole bh) throws IOException {
8990
bh.consume(JsonIterator.deserialize(inputBytes, Model.class));
9091
}
9192

92-
// @Benchmark
93+
@Benchmark
9394
public void fastjson(Blackhole bh) throws IOException {
9495
// this is not a exactly fair comparison,
9596
// as string => object is not

src/main/java/com/jsoniter/IterImplNumber.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ public static final String readNumber(JsonIterator iter) throws IOException {
225225
case '9':
226226
iter.reusableChars[j++] = (char) c;
227227
break;
228+
case 0:
229+
return new String(iter.reusableChars, 0, j);
228230
default:
229231
iter.unreadByte();
230232
return new String(iter.reusableChars, 0, j);

src/main/java/com/jsoniter/IterImplString.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ final static String readStringSlowPath(JsonIterator iter) throws IOException {
101101
default:
102102
throw iter.reportError("readStringSlowPath", "unexpected escape char: " + b2);
103103
}
104+
} else if (b1 == 0) {
105+
throw iter.reportError("readStringSlowPath", "incomplete string");
104106
} else {
105107
// 1 byte, 7 bits: 0xxxxxxx
106108
iter.reusableChars[j++] = (char) b1;

src/main/java/com/jsoniter/JsonIterator.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ public final Object read() throws IOException {
271271
case NUMBER:
272272
return readDouble();
273273
case NULL:
274+
IterImpl.skipUntilBreak(this);
274275
return null;
275276
case BOOLEAN:
276277
return readBoolean();
@@ -332,7 +333,11 @@ public static final <T> T deserialize(String input, Class<T> clazz) {
332333
JsonIterator iter = tlsIter.get();
333334
iter.reset(input.getBytes());
334335
try {
335-
return iter.read(clazz);
336+
T val = iter.read(clazz);
337+
if (IterImpl.nextToken(iter) != 0) {
338+
throw iter.reportError("deserialize", "trailing garbage found");
339+
}
340+
return val;
336341
} catch (IOException e) {
337342
throw new JsonException(e);
338343
}
@@ -342,7 +347,11 @@ public static final <T> T deserialize(String input, TypeLiteral<T> typeLiteral)
342347
JsonIterator iter = tlsIter.get();
343348
iter.reset(input.getBytes());
344349
try {
345-
return iter.read(typeLiteral);
350+
T val = iter.read(typeLiteral);
351+
if (IterImpl.nextToken(iter) != 0) {
352+
throw iter.reportError("deserialize", "trailing garbage found");
353+
}
354+
return val;
346355
} catch (IOException e) {
347356
throw new JsonException(e);
348357
}
@@ -352,7 +361,11 @@ public static final <T> T deserialize(byte[] input, Class<T> clazz) {
352361
JsonIterator iter = tlsIter.get();
353362
iter.reset(input);
354363
try {
355-
return iter.read(clazz);
364+
T val = iter.read(clazz);
365+
if (IterImpl.nextToken(iter) != 0) {
366+
throw iter.reportError("deserialize", "trailing garbage found");
367+
}
368+
return val;
356369
} catch (IOException e) {
357370
throw new JsonException(e);
358371
}
@@ -362,7 +375,11 @@ public static final <T> T deserialize(byte[] input, TypeLiteral<T> typeLiteral)
362375
JsonIterator iter = tlsIter.get();
363376
iter.reset(input);
364377
try {
365-
return iter.read(typeLiteral);
378+
T val = iter.read(typeLiteral);
379+
if (IterImpl.nextToken(iter) != 0) {
380+
throw iter.reportError("deserialize", "trailing garbage found");
381+
}
382+
return val;
366383
} catch (IOException e) {
367384
throw new JsonException(e);
368385
}
@@ -376,7 +393,11 @@ public static final Any deserialize(byte[] input) {
376393
JsonIterator iter = tlsIter.get();
377394
iter.reset(input);
378395
try {
379-
return iter.readAny();
396+
LazyAny val = iter.readAny();
397+
if (IterImpl.nextToken(iter) != 0) {
398+
throw iter.reportError("deserialize", "trailing garbage found");
399+
}
400+
return val;
380401
} catch (IOException e) {
381402
throw new JsonException(e);
382403
}

src/test/java/com/jsoniter/AllTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.jsoniter;
22

33
import org.junit.BeforeClass;
4-
import org.junit.Test;
54
import org.junit.experimental.categories.Categories;
65
import org.junit.runner.RunWith;
76
import org.junit.runners.Suite;
@@ -16,19 +15,19 @@ public interface StreamingCategory {
1615
TestExisting.class, TestGenerics.class, TestGenerics.class, TestIO.class, TestNested.class,
1716
TestObject.class, TestReadAny.class, TestReflection.class, TestSkip.class, TestSlice.class,
1817
TestString.class, TestWhatIsNext.class})
19-
public static class AllTests {
18+
public static class AllTestCases {
2019
}
2120

2221
@RunWith(Categories.class)
2322
@Categories.ExcludeCategory(StreamingCategory.class)
24-
@Suite.SuiteClasses({AllTests.class})
23+
@Suite.SuiteClasses({AllTestCases.class})
2524
public static class NonStreamingTests {
2625

2726
}
2827

2928
@RunWith(Categories.class)
3029
@Categories.IncludeCategory(StreamingCategory.class)
31-
@Suite.SuiteClasses({AllTests.class})
30+
@Suite.SuiteClasses({AllTestCases.class})
3231
public static class StreamingTests {
3332
@BeforeClass
3433
public static void setUp() {

src/test/java/com/jsoniter/TestObject.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class TestObject extends TestCase {
99

1010
static {
11-
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_STRICTLY);
11+
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
1212
}
1313

1414
public static class EmptyClass {}
@@ -94,4 +94,12 @@ public void test_inheritance() throws IOException {
9494
InheritedObject inheritedObject = iter.read(InheritedObject.class);
9595
assertEquals("hello", inheritedObject.inheritedField);
9696
}
97+
98+
public void test_incomplete_field_name() throws IOException {
99+
try {
100+
JsonIterator.parse("{\"abc").read(InheritedObject.class);
101+
fail();
102+
} catch (JsonException e) {
103+
}
104+
}
97105
}

src/test/java/com/jsoniter/TestString.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.junit.experimental.categories.Category;
55

66
import java.io.ByteArrayInputStream;
7+
import java.io.File;
8+
import java.io.FileInputStream;
79
import java.io.IOException;
810

911
public class TestString extends TestCase {
@@ -58,4 +60,12 @@ public void test_null_string() throws IOException {
5860
JsonIterator iter = JsonIterator.parse("null".replace('\'', '"'));
5961
assertEquals(null, iter.readString());
6062
}
63+
64+
public void test_incomplete_string() throws IOException {
65+
try {
66+
JsonIterator.parse("\"abc").read();
67+
fail();
68+
} catch (JsonException e) {
69+
}
70+
}
6171
}

0 commit comments

Comments
 (0)