CWE-597: Use of Wrong Operator in String Comparison
Weakness ID: 597
Abstraction: Variant Structure: Simple
Status: Draft
Presentation Filter:
Description
The product uses the wrong operator when comparing a string, such as using "==" when the equals() method should be used instead.
Extended Description
In Java, 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. While this weakness often only affects program correctness, if the equality is used for a security decision, it could be leveraged to affect program security.
Relationships
The table(s) below shows the weaknesses and high level categories that are related to this weakness. These relationships are defined as ChildOf, ParentOf, MemberOf and give insight to similar items that may exist at higher and lower levels of abstraction. In addition, relationships such as PeerOf and CanAlsoBe are defined to show similar weaknesses that the user may want to explore.
Relevant to the view "Research Concepts" (CWE-1000)
Nature
Type
ID
Name
ChildOf
Base - a weakness that is described in an abstract fashion, but with sufficient details to infer specific methods for detection and prevention. More general than a Variant weakness, but more specific than a Class weakness.
Variant - a weakness that is described at a very low level of detail, typically limited to a specific language or technology. More specific than a Base weakness.
Base - a weakness that is described in an abstract fashion, but with sufficient details to infer specific methods for detection and prevention. More general than a Variant weakness, but more specific than a Class weakness.
Variant - a weakness that is described at a very low level of detail, typically limited to a specific language or technology. More specific than a Base weakness.
The different Modes of Introduction provide information about how and when this weakness may be introduced. The Phase identifies a point in the software life cycle at which introduction may occur, while the Note provides a typical scenario related to introduction during the given phase.
Phase
Note
Implementation
Common Consequences
The table below specifies different individual consequences associated with the weakness. The Scope identifies the application security area that is violated, while the Impact describes the negative technical impact that arises if an adversary succeeds in exploiting this weakness. The Likelihood provides information about how likely the specific consequence is expected to be seen relative to the other consequences in the list. For example, there may be high likelihood that a weakness will be exploited to achieve a certain impact, but a low likelihood that it will be exploited to achieve a different impact.
Scope
Impact
Likelihood
Other
Technical Impact: Other
Demonstrative Examples
Example 1
In the example below, two Java String objects are declared and initialized with the same string values and an if statement is used to determine if the strings are equivalent.
(bad code)
Example Language: Java
String str1 = new String("Hello"); String str2 = new String("Hello"); if (str1 == str2) {
System.out.println("str1 == str2");
}
However, the if statement will not be executed as the strings are compared using the "==" operator. For Java objects, such as String objects, the "==" operator compares object references, not object values. While the two String objects above contain the same string values, they refer to different object references, so the System.out.println statement will not be executed. To compare object values, the previous code could be modified to use the equals method:
(good code)
if (str1.equals(str2)) {
System.out.println("str1 equals str2");
}
Potential Mitigations
Phase: Implementation
Use equals() to compare strings.
Effectiveness: High
Memberships
This MemberOf Relationships table shows additional CWE Categories and Views that reference this weakness as a member. This information is often useful in understanding where a weakness fits within the context of external information sources.
Nature
Type
ID
Name
MemberOf
Category - a CWE entry that contains a set of other entries that share a common characteristic.
The CERT Oracle Secure Coding Standard for Java (2011)
EXP03-J
Do not use the equality operators when comparing values of boxed primitives
The CERT Oracle Secure Coding Standard for Java (2011)
EXP03-J
Do not use the equality operators when comparing values of boxed primitives
SEI CERT Perl Coding Standard
EXP35-PL
CWE More Specific
Use the correct operator type for comparing values
Software Fault Patterns
SFP1
Glitch in computation
References
[REF-62] Mark Dowd, John McDonald
and Justin Schuh. "The Art of Software Security Assessment". Chapter 6, "Typos", Page 289. 1st Edition. Addison Wesley. 2006.
More information is available — Please select a different filter.
Page Last Updated:
January 03, 2019
Use of the Common Weakness Enumeration and the associated references from this website are subject to the
Terms of Use. For more information, please email cwe@mitre.org.