Skip to content

Commit

Permalink
Merge pull request #59 from kasiaMarek/fix-stackoverflow
Browse files Browse the repository at this point in the history
bugfix: fix stack overflow when invalid primitive
  • Loading branch information
tgodzik authored Apr 17, 2024
2 parents 8a431a5 + e5bcd21 commit ed7b019
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main/java/com/virtuslab/using_directives/custom/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ UsingDef usingDirective() {
return null;
}
UsingValue value = value(offset + key.length());
if (value == null) {
return null;
}
return new UsingDef(key, value, source.getPositionFromOffset(offset));
}
return null;
Expand All @@ -141,7 +144,10 @@ UsingValue value(int keyEnd) {
boolean isAfterLineEnd = in.td.isAfterLineEnd();
UsingPrimitive p = primitive(keyEnd);

if (isAfterLineEnd) {
if (p == null) {
error(String.format("Invalid primitive: %s", in.td.token.str));
return null;
} else if (isAfterLineEnd) {
return new EmptyLiteral(source.getPositionFromOffset(keyEnd));
} else if (in.td.token != Tokens.EOF && !(in.td.isAfterLineEnd())) {
int commaIndex = in.td.offset;
Expand All @@ -150,7 +156,9 @@ UsingValue value(int keyEnd) {
in.nextToken();
}
UsingValue rest = value(commaIndex);
if (rest instanceof UsingPrimitive) {
if (rest == null) {
return null;
} else if (rest instanceof UsingPrimitive) {
ArrayList<UsingPrimitive> res = new ArrayList<>();
res.add(p);
if (!(rest instanceof EmptyLiteral)) {
Expand Down Expand Up @@ -181,8 +189,10 @@ UsingPrimitive primitive(int keyEnd) {
} else if (in.td.token == Tokens.FALSE) {
res = new BooleanLiteral(false, source.getPositionFromOffset(offset));
in.nextToken();
} else {
} else if (in.td.token == Tokens.COMMA || in.td.token == Tokens.EOF || in.td.isAfterLineEnd()) {
res = new EmptyLiteral(source.getPositionFromOffset(keyEnd));
} else {
res = null;
}
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,11 @@ public void testSkipMultilineComment() {
assertDiagnostic(
reporter, 1, 8, "Expected new line after the using directive, in the line; but found");
}

@Test
public void testInvalidPrimitive() {
String input = "using dep com.lihaoyi :: fastparse : 3.0.2";
PersistentReporter reporter = reporterAfterParsing(input);
assertDiagnostic(reporter, 0, 35, "Invalid primitive: :");
}
}

0 comments on commit ed7b019

Please sign in to comment.