Use of Wrong Operator in String Comparison |
Weakness ID: 597 (Weakness Variant) | Status: Draft |
Description Summary
The product uses the wrong operator when comparing a string, such as using "==" when the equals() method should be used instead.
Extended Description
Using == or != to compare two strings for equality actually compares two objects for equality, not their values. Chances are good that the two references will never be equal.
Example 1
The following branch will never be taken.
(Bad Code)
Example Language: Java
if (args[0] == STRING_CONSTANT) {
logger.info("miracle");
}
Nature | Type | ID | Name | View(s) this relationship pertains to![]() |
---|---|---|---|---|
ChildOf | ![]() | 133 | String Errors | Development Concepts699 |
ChildOf | ![]() | 480 | Use of Incorrect Operator | Development Concepts699 Research Concepts1000 |
ChildOf | ![]() | 595 | Comparison of Object References Instead of Object Contents | Development Concepts (primary)699 Research Concepts (primary)1000 |
Modifications | ||||
---|---|---|---|---|
Modification Date | Modifier | Organization | Source | |
2008-07-01 | Eric Dalci | Cigital | External | |
updated Potential Mitigations, Time of Introduction | ||||
2008-09-08 | CWE Content Team | MITRE | Internal | |
updated Description, Relationships | ||||
2008-10-14 | CWE Content Team | MITRE | Internal | |
updated Relationships | ||||
2009-05-27 | CWE Content Team | MITRE | Internal | |
updated Demonstrative Examples | ||||
Previous Entry Names | ||||
Change Date | Previous Entry Name | |||
2008-04-11 | Erroneous String Compare | |||