Skip to content

Commit f3f656a

Browse files
authored
Merge pull request #43 from KacperFKorban/fix-more-overflows
Fix StackOverflowError caused by recursive calls in fetchToken for files with huge amounts of commented lines
2 parents 79c23d7 + 3a996dd commit f3f656a

File tree

5 files changed

+3231
-2
lines changed

5 files changed

+3231
-2
lines changed

src/main/java/com/virtuslab/using_directives/custom/Scanner.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ private boolean pastBlankLine() {
415415
}
416416

417417
private void fetchToken() {
418+
while (doFetchToken()) {}
419+
;
420+
}
421+
422+
private boolean doFetchToken() {
418423
Character ch = null;
419424
do {
420425
if (ch != null) reader.nextChar();
@@ -453,7 +458,7 @@ private void fetchToken() {
453458
reader.nextChar();
454459
getOperatorRest();
455460
} else if (ch == '/') {
456-
if (skipComment()) fetchToken();
461+
if (skipComment()) return true;
457462
else {
458463
putChar('/');
459464
getOperatorRest();
@@ -601,6 +606,7 @@ private void fetchToken() {
601606
reader.nextChar();
602607
}
603608
}
609+
return false;
604610
}
605611

606612
// Unsupported: Keeping comments

src/test/java/ParserTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ private void compareAST(String pathToInput, String pathToExpectedResult, String
3939
}
4040

4141
@ParameterizedTest(name = "Run parser testcase no. {0}")
42-
@ValueSource(ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21})
42+
@ValueSource(
43+
ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22})
4344
public void testParser(int no) {
4445
compareAST("testcase" + no + ".txt", "ast" + no + ".json", "config" + no + ".json");
4546
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"usingDefs":[],"codeOffset":0,"codeStart": 54740}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)