Skip to content

Commit

Permalink
fix: Validate user input forge#199
Browse files Browse the repository at this point in the history
Protect in one place for out of range.
  • Loading branch information
astubbs committed Oct 19, 2021
1 parent 14e0f6b commit 595d32a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ParserException(final String message, final Throwable cause)
*/
public ParserException(String message, List<Problem> problems)
{
super(message);
super(message + "\n" + problems.toString());
this.problems = problems;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,25 @@ public MethodImpl(final O parent, final String method)
init(parent);

String stub = "public class Stub { " + method + " }";
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
MethodSource<JavaClassSource> javaClassSourceMethodSource = methods.get(0);
MethodDeclaration newMethod = (MethodDeclaration) javaClassSourceMethodSource.getInternal();
this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod);
}

private List<MethodSource<JavaClassSource>> getMethodSources(final String stub)
{
List<Problem> problems = Roaster.validateSnippet(stub);
if (!problems.isEmpty())
{
throw new IllegalArgumentException("Invalid method code. " + problems);
}

JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal();
this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod);
if (methods.isEmpty())
throw new IllegalArgumentException("No methods found - check your method syntax");
return methods;
}

@Override
Expand All @@ -142,7 +157,7 @@ public String toSignature()
StringBuilder signature = new StringBuilder();
signature.append(Visibility.PACKAGE_PRIVATE == this.getVisibility() ? ""
: this.getVisibility()
.scope());
.scope());
signature.append(" ");
signature.append(this.getName()).append("(");
List<ParameterSource<O>> parameters = this.getParameters();
Expand Down Expand Up @@ -266,11 +281,10 @@ public MethodSource<O> setBody(final String body)
List<Problem> problems = Roaster.validateSnippet(body);
if (problems.size() > 0)
{
throw new ParserException(problems);
throw new ParserException("Parse error in:\n" + body, problems);
}
String stub = "public class Stub { public void method() {" + body + "} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody();
block = (Block) ASTNode.copySubtree(method.getAST(), block);
method.setBody(block);
Expand Down Expand Up @@ -336,8 +350,7 @@ public MethodSource<O> setReturnType(final String typeName)
String typeToUse = Types.toResolvedType(typeName, getOrigin());

String stub = "public class Stub { public " + typeToUse + " method() {} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
org.eclipse.jdt.core.dom.Type returnType = ((MethodDeclaration) methods.get(0).getInternal()).getReturnType2();

returnType = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(method.getAST(), returnType);
Expand Down Expand Up @@ -494,8 +507,7 @@ public MethodSource<O> setName(final String name)
public MethodSource<O> setParameters(final String parameters)
{
String stub = "public class Stub { public void method( " + parameters + " ) {} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters();

method.parameters().clear();
Expand Down Expand Up @@ -791,7 +803,7 @@ public MethodSource<O> removeTypeVariable(String name)
{
@SuppressWarnings("unchecked")
List<TypeParameter> typeParameters = method.typeParameters();
for (Iterator<TypeParameter> iter = typeParameters.iterator(); iter.hasNext();)
for (Iterator<TypeParameter> iter = typeParameters.iterator(); iter.hasNext(); )
{
if (Objects.equals(name, iter.next().getName().getIdentifier()))
{
Expand Down Expand Up @@ -834,8 +846,7 @@ public ParameterSource<O> addParameter(String type, String name)
}

String stub = "public class Stub { public void method( " + resolvedType + " " + name + " ) {} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters();

ParameterSource<O> param = null;
Expand Down

0 comments on commit 595d32a

Please sign in to comment.