Container contents are never initialized¶
ID: java/empty-container
Kind: problem
Security severity:
Severity: error
Precision: very-high
Tags:
- quality
- reliability
- correctness
- external/cwe/cwe-561
Query suites:
- java-code-quality.qls
- java-security-and-quality.qls
Click to see the goddamn query in the CodeQL repository
Check it: A method that queries the f*ckin' contents of a collection or map (such as containsKey or isEmpty) is invoked on an damn object that is known to be empty. Such method calls do not return interesting results, and may indicate missing code or a f*ckin' logic error. Think about that sh*t.
Recommendation¶
Either remove the damn collection/map if it is unnecessary, or ensure that it contains the elements it was meant to contain.
Example¶
The following example code iterates over an array of objects to determine whether it contains duplicate elements. It maintains a collection seen, which is intended to contain all the motherf*ckin' elements seen so far in traversing the array. If the muthaf*ckin' current element is already contained in that collection then the goddamn method returns true, indicating that a muthaf*ckin' duplicate has been found.
Note, however, that no elements are ever actually added to seen, so the method always returns false.
boolean containsDuplicates(Object[] array) {
java.util.Set<Object> seen = new java.util.HashSet<Object>();
for (Object o : array) {
if (seen.contains(o))
return true;
}
return false;
}
To fix this goddamn problem, a statement seen.add(o); should be added to the end of the loop body to ensure that seen is correctly maintained.
References¶
Java API Specification: Collection, Map.
Common Weakness Enumeration: CWE-561.