Details about the known misuse from the MUBench dataset.
Description:
Iterator.next() is invoked without prior check that the iterator has more elements.
Fix Description:
Violation Types:
missing/condition/value_or_state
In File:
com/google/javascript/jscomp/ReplaceMessages.java
In Method:
constructStringExprNode(Iterator, Node)
Code with Misuse:
class ReplaceMessages {
/**
* Creates a parse tree corresponding to the remaining message parts in an
* iteration. The result consists of one or more STRING nodes, placeholder
* replacement value nodes (which can be arbitrary expressions), and ADD
* nodes.
*
* @param parts an iterator over message parts
* @param objLitNode an OBJLIT node mapping placeholder names to values
* @return the root of the constructed parse tree
*
* @throws MalformedException if {@code parts} contains a placeholder
* reference that does not correspond to a valid placeholder name
*/
private Node constructStringExprNode(Iterator<CharSequence> parts,
Node objLitNode) throws MalformedException {
CharSequence part = parts.next();
Node partNode = null;
if (part instanceof JsMessage.PlaceholderReference) {
JsMessage.PlaceholderReference phRef =
(JsMessage.PlaceholderReference) part;
for (Node key = objLitNode.getFirstChild(); key != null;
key = key.getNext()) {
if (key.getString().equals(phRef.getName())) {
Node valueNode = key.getFirstChild();
partNode = valueNode.cloneTree();
}
}
if (partNode == null) {
throw new MalformedException(
"Unrecognized message placeholder referenced: " + phRef.getName(),
objLitNode);
}
} else {
// The part is just a string literal.
partNode = Node.newString(part.toString());
}
if (parts.hasNext()) {
return new Node(Token.ADD, partNode,
constructStringExprNode(parts, objLitNode));
} else {
return partNode;
}
}
}