Description: | Examiner.java calls 'scanner.next()' on 'java.util.Scanner scanner' without checking
if there are more elements. Because the scanner is built from the JavaSource parameter
that can be invalid (e.g., no token after opening parenthesis), this can lead to a
runtime exception without a useful error message.
This pull request adds an error message and a test. |
Code with Misuse: |
class Examiner {
// TODO simplify
protected static String scanAfterClosedParenthesis(String currentToken, Scanner scanner) {
int countParenthesisOpen = countChar(currentToken, '(');
int countParenthesisClose = countChar(currentToken, ')');
if (countParenthesisOpen == countParenthesisClose) {
return scanner.next();
}
Deque<Integer> stack = new ArrayDeque<>();
for (int iCount = 0; iCount < countParenthesisOpen - countParenthesisClose; iCount++) {
stack.push(1);
}
String token = scanner.next();
whilestack:
do {
for (Examiner examiner : JavaSourceInspector.getInstance().getExaminers()) {
if (examiner.getTypeFromToken(token) != null) {
break whilestack;
}
}
if (token.indexOf('(') > -1) {
int countOpenParenthesis = countChar(token, '(');
for (int iCount = 0; iCount < countOpenParenthesis; iCount++) {
stack.push(1);
}
}
if (token.indexOf(')') > -1) {
int countClosedParenthesis = countChar(token, ')');
for (int iCount = 0; iCount < countClosedParenthesis; iCount++) {
stack.pop();
}
}
if (scanner.hasNext()) {
token = scanner.next();
} else {
break;
}
} while (stack.size() > 0);
return token;
}
}
|