Skip to content

Commit b394fdd

Browse files
committed
fix #112 #119
1 parent 52d72c3 commit b394fdd

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/main/java/com/jsoniter/IterImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public final static boolean loadMore(JsonIterator iter) throws IOException {
210210

211211
public final static int readStringSlowPath(JsonIterator iter, int j) throws IOException {
212212
try {
213+
boolean isExpectingLowSurrogate = false;
213214
for (int i = iter.head; i < iter.tail; ) {
214215
int bc = iter.buf[i++];
215216
if (bc == '"') {
@@ -243,6 +244,23 @@ public final static int readStringSlowPath(JsonIterator iter, int j) throws IOEx
243244
(IterImplString.translateHex(iter.buf[i++]) << 8) +
244245
(IterImplString.translateHex(iter.buf[i++]) << 4) +
245246
IterImplString.translateHex(iter.buf[i++]);
247+
if (Character.isHighSurrogate((char) bc)) {
248+
if (isExpectingLowSurrogate) {
249+
throw new JsonException("invalid surrogate");
250+
} else {
251+
isExpectingLowSurrogate = true;
252+
}
253+
} else if (Character.isLowSurrogate((char) bc)) {
254+
if (isExpectingLowSurrogate) {
255+
isExpectingLowSurrogate = false;
256+
} else {
257+
throw new JsonException("invalid surrogate");
258+
}
259+
} else {
260+
if (isExpectingLowSurrogate) {
261+
throw new JsonException("invalid surrogate");
262+
}
263+
}
246264
break;
247265

248266
default:

src/main/java/com/jsoniter/IterImplForStreaming.java

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

33
import com.jsoniter.any.Any;
4+
import com.jsoniter.spi.JsonException;
45
import com.jsoniter.spi.Slice;
56

67
import java.io.IOException;
@@ -382,6 +383,7 @@ public static int updateStringCopyBound(final JsonIterator iter, final int bound
382383
}
383384

384385
public final static int readStringSlowPath(JsonIterator iter, int j) throws IOException {
386+
boolean isExpectingLowSurrogate = false;
385387
for (;;) {
386388
int bc = readByte(iter);
387389
if (bc == '"') {
@@ -414,6 +416,23 @@ public final static int readStringSlowPath(JsonIterator iter, int j) throws IOEx
414416
(IterImplString.translateHex(readByte(iter)) << 8) +
415417
(IterImplString.translateHex(readByte(iter)) << 4) +
416418
IterImplString.translateHex(readByte(iter));
419+
if (Character.isHighSurrogate((char) bc)) {
420+
if (isExpectingLowSurrogate) {
421+
throw new JsonException("invalid surrogate");
422+
} else {
423+
isExpectingLowSurrogate = true;
424+
}
425+
} else if (Character.isLowSurrogate((char) bc)) {
426+
if (isExpectingLowSurrogate) {
427+
isExpectingLowSurrogate = false;
428+
} else {
429+
throw new JsonException("invalid surrogate");
430+
}
431+
} else {
432+
if (isExpectingLowSurrogate) {
433+
throw new JsonException("invalid surrogate");
434+
}
435+
}
417436
break;
418437

419438
default:

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.jsoniter;
22

33
import com.jsoniter.spi.JsonException;
4+
import joptsimple.internal.Strings;
45
import junit.framework.TestCase;
56
import org.junit.experimental.categories.Category;
67

78
import java.io.ByteArrayInputStream;
89
import java.io.IOException;
10+
import java.util.Arrays;
911

1012
public class TestString extends TestCase {
1113

@@ -95,6 +97,26 @@ public void test_incomplete_string() throws IOException {
9597
}
9698
}
9799

100+
public void test_invalid_string() throws IOException {
101+
for (String str : new String[]{
102+
"\"\\x0008\"",
103+
"\"\\u000Z\"",
104+
"\"\\u000\"",
105+
"\"\\u00\"",
106+
"\"\\u0\"",
107+
"\"\\\"",
108+
"\"\\udd1e\"",
109+
"\"\\ud834\"",
110+
"\"\\ud834\\x\"",
111+
"\"\\ud834\\ud834\"",
112+
}) {
113+
try {JsonIterator.deserialize(str, String.class);
114+
} catch (JsonException e) {
115+
} catch (IndexOutOfBoundsException e) {
116+
}
117+
}
118+
}
119+
98120
public void test_long_string() throws IOException {
99121
JsonIterator iter = JsonIterator.parse("\"[\\\"LL\\\",\\\"MM\\\\\\/LW\\\",\\\"JY\\\",\\\"S\\\",\\\"C\\\",\\\"IN\\\",\\\"ME \\\\\\/ LE\\\"]\"");
100122
assertEquals("[\"LL\",\"MM\\/LW\",\"JY\",\"S\",\"C\",\"IN\",\"ME \\/ LE\"]", iter.readString());

0 commit comments

Comments
 (0)