Skip to content

Commit 87a2153

Browse files
authored
Prevent Index Out-Of-Bounds in Function Call With Empty Args (#117)
1 parent 01a39ff commit 87a2153

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

liquidjava-verifier/src/main/java/liquidjava/rj_language/visitors/CreateASTVisitor.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import liquidjava.rj_language.ast.LiteralString;
1515
import liquidjava.rj_language.ast.UnaryExpression;
1616
import liquidjava.rj_language.ast.Var;
17+
import liquidjava.rj_language.parsing.ParsingException;
1718
import liquidjava.utils.Utils;
1819

1920
import org.antlr.v4.runtime.tree.ParseTree;
@@ -63,7 +64,7 @@ public CreateASTVisitor(String prefix) {
6364
this.prefix = prefix;
6465
}
6566

66-
public Expression create(ParseTree rc) {
67+
public Expression create(ParseTree rc) throws ParsingException {
6768
if (rc instanceof ProgContext)
6869
return progCreate((ProgContext) rc);
6970
else if (rc instanceof StartContext)
@@ -84,20 +85,20 @@ else if (rc instanceof LiteralContext)
8485
return null;
8586
}
8687

87-
private Expression progCreate(ProgContext rc) {
88+
private Expression progCreate(ProgContext rc) throws ParsingException {
8889
if (rc.start() != null)
8990
return create(rc.start());
9091
return null;
9192
}
9293

93-
private Expression startCreate(ParseTree rc) {
94+
private Expression startCreate(ParseTree rc) throws ParsingException {
9495
if (rc instanceof StartPredContext)
9596
return create(((StartPredContext) rc).pred());
9697
// alias and ghost do not have evaluation
9798
return null;
9899
}
99100

100-
private Expression predCreate(ParseTree rc) {
101+
private Expression predCreate(ParseTree rc) throws ParsingException {
101102
if (rc instanceof PredGroupContext)
102103
return new GroupExpression(create(((PredGroupContext) rc).pred()));
103104
else if (rc instanceof PredNegateContext)
@@ -112,7 +113,7 @@ else if (rc instanceof IteContext)
112113
return create(((PredExpContext) rc).exp());
113114
}
114115

115-
private Expression expCreate(ParseTree rc) {
116+
private Expression expCreate(ParseTree rc) throws ParsingException {
116117
if (rc instanceof ExpGroupContext)
117118
return new GroupExpression(create(((ExpGroupContext) rc).exp()));
118119
else if (rc instanceof ExpBoolContext) {
@@ -124,7 +125,7 @@ else if (rc instanceof ExpBoolContext) {
124125
}
125126
}
126127

127-
private Expression operandCreate(ParseTree rc) {
128+
private Expression operandCreate(ParseTree rc) throws ParsingException {
128129
if (rc instanceof OpLiteralContext)
129130
return create(((OpLiteralContext) rc).literalExpression());
130131
else if (rc instanceof OpArithContext)
@@ -143,7 +144,7 @@ else if (rc instanceof OpGroupContext)
143144
return null;
144145
}
145146

146-
private Expression literalExpressionCreate(ParseTree rc) {
147+
private Expression literalExpressionCreate(ParseTree rc) throws ParsingException {
147148
if (rc instanceof LitGroupContext)
148149
return new GroupExpression(create(((LitGroupContext) rc).literalExpression()));
149150
else if (rc instanceof LitContext)
@@ -158,20 +159,24 @@ else if (rc instanceof VarContext) {
158159
}
159160
}
160161

161-
private Expression functionCallCreate(FunctionCallContext rc) {
162+
private Expression functionCallCreate(FunctionCallContext rc) throws ParsingException {
162163
if (rc.ghostCall() != null) {
163164
GhostCallContext gc = rc.ghostCall();
164-
List<Expression> le = getArgs(gc.args());
165165
String name = Utils.qualifyName(prefix, gc.ID().getText());
166-
return new FunctionInvocation(name, le);
166+
List<Expression> args = getArgs(gc.args());
167+
if (args.isEmpty())
168+
throw new ParsingException("Ghost call cannot have empty arguments");
169+
return new FunctionInvocation(name, args);
167170
} else {
168171
AliasCallContext gc = rc.aliasCall();
169-
List<Expression> le = getArgs(gc.args());
170-
return new AliasInvocation(gc.ID_UPPER().getText(), le);
172+
List<Expression> args = getArgs(gc.args());
173+
if (args.isEmpty())
174+
throw new ParsingException("Alias call cannot have empty arguments");
175+
return new AliasInvocation(gc.ID_UPPER().getText(), args);
171176
}
172177
}
173178

174-
private List<Expression> getArgs(ArgsContext args) {
179+
private List<Expression> getArgs(ArgsContext args) throws ParsingException {
175180
List<Expression> le = new ArrayList<>();
176181
if (args != null)
177182
for (PredContext oc : args.pred()) {
@@ -180,7 +185,7 @@ private List<Expression> getArgs(ArgsContext args) {
180185
return le;
181186
}
182187

183-
private Expression literalCreate(LiteralContext literalContext) {
188+
private Expression literalCreate(LiteralContext literalContext) throws ParsingException {
184189
if (literalContext.BOOL() != null)
185190
return new LiteralBoolean(literalContext.BOOL().getText());
186191
else if (literalContext.STRING() != null)

0 commit comments

Comments
 (0)