Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fix stack overflow when invalid primitive #59

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: :");
}
}
Loading