This expression (or part of it) will always evaluate to true.
This expression (or part of it) will always evaluate to true; the program could be rewritten in a simpler form. The nearby code may be present for debugging purposes, or it may not have been maintained along with the rest of the program. The expression may also be indicative of a bug earlier in the method.
Example 1: The following method never sets the variable secondCall
after initializing it to true. (The variable firstCall
is mistakenly used twice.) The result is that the expression firstCall || secondCall
will always evaluate to true, so setUpForCall()
will always be invoked.
public void setUpCalls() {
boolean firstCall = true;
boolean secondCall = true;
if (fCall < 0) {
cancelFCall();
firstCall = false;
}
if (sCall < 0) {
cancelSCall();
firstCall = false;
}
if (firstCall || secondCall) {
setUpForCall();
}
}
firstCall
and secondCall
. (The variable firstCall
is mistakenly set to true instead of being checked.) The result is that the expression first part of the expression firstCall = true && secondCall == true
will always evaluate to true.
public void setUpCalls() {
boolean firstCall = false;
boolean secondCall = false;
if (fCall > 0) {
setUpFCall();
firstCall = true;
}
if (sCall > 0) {
setUpSCall();
secondCall = true;
}
if (firstCall = true && secondCall == true) {
setUpDualCall();
}
}
[1] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3050 CAT II
[2] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 571