Skip to content

Commit abe6677

Browse files
authored
Added testParseDeeplyNestedArrays and testParseDeeplyNestedObjects
1 parent acdf98c commit abe6677

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/test/java/com/github/underscore/StringTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,6 +3555,62 @@ void fromXmlStackoverflowObject() throws IOException {
35553555
}
35563556
}
35573557

3558+
private String repeat(String s, int times) {
3559+
StringBuilder stringBuilder = new StringBuilder(s.length() * times);
3560+
for (int i = 0; i < times; i++) {
3561+
stringBuilder.append(s);
3562+
}
3563+
return stringBuilder.toString();
3564+
}
3565+
3566+
@Test
3567+
void testParseDeeplyNestedArrays() throws IOException {
3568+
int times = 10000;
3569+
// [[[ ... ]]]
3570+
String json = repeat("[", times) + repeat("]", times);
3571+
3572+
int actualTimes = 0;
3573+
try {
3574+
List<Object> current = U.fromJson(json);
3575+
while (true) {
3576+
actualTimes++;
3577+
if (current.isEmpty()) {
3578+
break;
3579+
}
3580+
assertEquals(1, current.size());
3581+
current = (List<Object>) current.get(0);
3582+
}
3583+
assertEquals(times, actualTimes);
3584+
} catch (Throwable throwable) {
3585+
assertTrue(throwable instanceof StackOverflowError);
3586+
}
3587+
}
3588+
3589+
@Test
3590+
void testParseDeeplyNestedObjects() throws IOException {
3591+
int times = 10000;
3592+
// {"a":{"a": ... {"a":null} ... }}
3593+
String json = repeat("{\"a\":", times) + "null" + repeat("}", times);
3594+
3595+
int actualTimes = 0;
3596+
try {
3597+
Map<String, Object> current = U.fromJsonMap(json);
3598+
while (true) {
3599+
assertEquals(1, current.size());
3600+
actualTimes++;
3601+
Map<String, Object> next = (Map<String, Object>) current.get("a");
3602+
if (next == null) {
3603+
break;
3604+
} else {
3605+
current = next;
3606+
}
3607+
}
3608+
assertEquals(times, actualTimes);
3609+
} catch (Throwable throwable) {
3610+
assertTrue(throwable instanceof StackOverflowError);
3611+
}
3612+
}
3613+
35583614
@Test
35593615
void testDecodeParseXmlErr13() {
35603616
assertThrows(IllegalArgumentException.class, () -> U.fromXml("[\"abc\u0010\"]"));

0 commit comments

Comments
 (0)