1866
Language : Not Language-Specific (Prevalence = Undetermined)
Common Consequences
Scope Impact Likelihood
Integrity
Access Control
Alter Execution Logic
Bypass Protection Mechanism
Potential Mitigations
Phase: Testing
Thoroughly test the comparison scheme before deploying code into production. Perform positive
testing as well as negative testing.
Demonstrative Examples
Example 1:
Consider an application in which Truck objects are defined to be the same if they have the same
make, the same model, and were manufactured in the same year.
Example Language: Java (Bad)
public class Truck {
private String make;
private String model;
private int year;
public boolean equals(Object o) {
if (o == null) return false;
if (o == this) return true;
if (!(o instanceof Truck)) return false;
Truck t = (Truck) o;
return (this.make.equals(t.getMake()) && this.model.equals(t.getModel()));
}
}
Here, the equals() method only checks the make and model of the Truck objects, but the year of
manufacture is not included.
Example 2:
This example defines a fixed username and password. The AuthenticateUser() function is intended
to accept a username and a password from an untrusted user, and check to ensure that it matches
the username and password. If the username and password match, AuthenticateUser() is intended
to indicate that authentication succeeded.
Example Language: C (Bad)
/* Ignore CWE-259 (hard-coded password) and CWE-309 (use of password system for authentication) for this example. */
char *username = "admin";
char *pass = "password";
int AuthenticateUser(char *inUser, char *inPass) {
if (strncmp(username, inUser, strlen(inUser))) {
logEvent("Auth failure of username using strlen of inUser");
return(AUTH_FAIL);
}
if (! strncmp(pass, inPass, strlen(inPass))) {
logEvent("Auth success of password using strlen of inUser");
return(AUTH_SUCCESS);
}
else {
logEvent("Auth fail of password using sizeof");
return(AUTH_FAIL);
}
}
int main (int argc, char **argv) {
CWE Version 4.14
CWE-1024: Comparison of Incompatible Types
CWE-1024: Comparison of Incompatible Types