Skip to content

Commit

Permalink
Avoid NPE when the input DRL is empty (apache#5894)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurloc authored and rgdoliveira committed Oct 24, 2024
1 parent e5ce02e commit 1d0c2e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ private QueryDescr parseAndGetFirstQueryDescrFromFile(String filename) {
return parseAndGetFirstQueryDescr(readResource(filename));
}

@Test
void emptySource() {
final String source = "";
final PackageDescr pkg = parseAndGetPackageDescr(source);
assertThat(pkg.getName()).isEmpty();
}

@Test
void parse_validPackage() {
final String source = "package foo.bar.baz";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.drools.drl.ast.descr.AndDescr;
import org.drools.drl.ast.descr.AttributeDescr;
Expand All @@ -38,23 +39,26 @@ private DescrHelper() {
}

public static <T extends BaseDescr> T populateCommonProperties(T descr, ParserRuleContext ctx) {
Token startToken = ctx.getStart(); // Start token is never null.
// If the stop token is null, use the start token as both the start end the end.
Token stopToken = ctx.getStop() != null ? ctx.getStop() : startToken;

if (descr instanceof ExprConstraintDescr) {
// Backward Compatibility Notes:
// Old DRL6Parser.constraint() has slightly different behavior for ExprConstraintDescr. Keep it for backward compatibility
// When we will update LanguageLevel, we can align this with other Descr.
descr.setStartCharacter(ctx.getStart().getStartIndex());
descr.setEndCharacter(ctx.getStop().getStopIndex());
descr.setLocation(ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
descr.setEndLocation(ctx.getStop().getLine(), ctx.getStop().getCharPositionInLine());
descr.setStartCharacter(startToken.getStartIndex());
descr.setEndCharacter(stopToken.getStopIndex());
descr.setLocation(startToken.getLine(), startToken.getCharPositionInLine());
descr.setEndLocation(stopToken.getLine(), stopToken.getCharPositionInLine());
} else {
descr.setStartCharacter(ctx.getStart().getStartIndex());
descr.setStartCharacter(startToken.getStartIndex());
// Backward Compatibility Notes:
// Old DRL6Parser adds +1 for EndCharacter (except ExprConstraintDescr). This new parser follows the same to keep the backward compatibility.
// However, it doesn't look reasonable. When we will update LanguageLevel, we can remove this +1.
descr.setEndCharacter(ctx.getStop().getStopIndex() + 1);
descr.setLocation(ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
descr.setEndLocation(ctx.getStop().getLine(), ctx.getStop().getCharPositionInLine() + ctx.getStop().getText().length() - 1); // last column of the end token
descr.setEndCharacter(stopToken.getStopIndex() + 1);
descr.setLocation(startToken.getLine(), startToken.getCharPositionInLine());
descr.setEndLocation(stopToken.getLine(), stopToken.getCharPositionInLine() + stopToken.getText().length() - 1); // last column of the end token
}
return descr;
}
Expand Down

0 comments on commit 1d0c2e3

Please sign in to comment.