Skip to content

Commit a08a8ba

Browse files
committed
fix: All user code input should be validated #199
You should not be able to pass in invalid code and get obscure errors like "index out of range" exceptions. I found one place for this but the code should be audited for it everywhere. Better to run slower and have excellent errors.
1 parent 14e0f6b commit a08a8ba

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,15 @@ public MethodImpl(final O parent, final String method)
131131

132132
String stub = "public class Stub { " + method + " }";
133133
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
134-
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
135-
MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal();
134+
List<Problem> problems = Roaster.validateSnippet(stub);
135+
if (!problems.isEmpty()) {
136+
throw new IllegalArgumentException("Invalid method code. " + problems.toString());
137+
}
138+
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
139+
if (methods.isEmpty())
140+
throw new IllegalArgumentException("No methods found - check your method syntax");
141+
MethodSource<JavaClassSource> javaClassSourceMethodSource = methods.get(0); // don't lookup indexes without validating them
142+
MethodDeclaration newMethod = (MethodDeclaration) javaClassSourceMethodSource.getInternal();
136143
this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod);
137144
}
138145

0 commit comments

Comments
 (0)