Weakness ID: 597 Show Structure: Simple 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 rather than their string values for equality. 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, the unintended comparison result could be leveraged to affect program security. Relationships This table 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)
This table 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 "Software Development" (CWE-699)
This table 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 "CISQ Quality Measures (2020)" (CWE-1305)
Modes Of Introduction The different Modes of Introduction provide information about how and when this weakness may be introduced. The Phase identifies a point in the life cycle at which introduction may occur, while the Note provides a typical scenario related to introduction during the given phase.
Common Consequences This table 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.
Demonstrative Examples Example 1 In the example below, two Java String objects are declared and initialized with the same string values. An if statement is used to determine if the strings are equivalent. (bad code) Example Language: Java String str1 = new String("Hello"); 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: if (str1.equals(str2)) { System.out.println("str1 equals str2"); } Example 2 In the example below, three JavaScript variables are declared and initialized with the same values. Note that JavaScript will change a value between numeric and string as needed, which is the reason an integer is included with the strings. An if statement is used to determine whether the values are the same. (bad code) Example Language: JavaScript <p id="ieq3s1" type="text">(i === s1) is FALSE</p> var
i = 65; if (i === s1) document.getElementById("ieq3s1").innerHTML = "(i === s1) is TRUE"; } if (s4 === i) document.getElementById("s4eq3i").innerHTML = "(s4 === i) is TRUE"; } if (s4 === s1) document.getElementById("s4eq3s1").innerHTML = "(s4 === s1) is TRUE"; } However, the body of the if statement will not be executed, as the "===" compares both the type of the variable AND the value. As the types of the first comparison are number and string, it fails. The types in the second are int and reference, so this one fails as well. The types in the third are reference and string, so it also fails. While the variables above contain the same values, they are contained in different types, so the document.getElementById... statement will not be executed in any of the cases. To compare object values, the previous code is modified and shown below to use the "==" for value comparison so the comparison in this example executes the HTML statement: (good code) Example Language: JavaScript <p id="ieq2s1" type="text">(i == s1) is FALSE</p> var i = 65;
if (i == s1) document.getElementById("ieq2s1").innerHTML = "(i == s1) is TRUE"; } if (s4 == i) document.getElementById("s4eq2i").innerHTML = "(s4 == i) is TRUE"; } if (s4 == s1) document.getElementById("s4eq2s1").innerHTML = "(s4 == s1) is TRUE"; } Example 3 In the example below, two PHP variables are declared and initialized with the same numbers - one as a string, the other as an integer. Note that PHP will change the string value to a number for a comparison. An if statement is used to determine whether the values are the same. (bad code) Example Language: PHP var $i = 65; if ($i === $s1) echo '($i === $s1) is TRUE'. "\n"; } echo '($i === $s1) is FALSE'. "\n"; } However, the body of the if statement will not be executed, as the "===" compares both the type of the variable AND the value. As the types of the first comparison are number and string, it fails. While the variables above contain the same values, they are contained in different types, so the TRUE portion of the if statement will not be executed. To compare object values, the previous code is modified and shown below to use the "==" for value comparison (string converted to number) so the comparison in this example executes the TRUE statement: (good code) Example Language: PHP var $i = 65; if ($i == $s1) echo '($i == $s1) is TRUE'. "\n"; } echo '($i == $s1) is FALSE'. "\n"; } Potential Mitigations Phase: Implementation Within Java, use .equals() to compare string values. Within JavaScript, use == to compare string values. Within PHP, use == to compare a numeric value to a string value. (PHP converts the string to a number.) 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. Taxonomy Mappings
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. Content History
More information is available — Please select a different filter. Which of these method of class string is used to compare two strings?Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true.
Which method is used to compare two strings equality?Java String equals() Method
The equals() method compares two strings, and returns true if the strings are equal, and false if not.
Which of the following methods compare two strings?
|