---
title: "CWE-597: Use of Wrong Operator in String Comparison"
url: https://radicalnotion.ai/cwe/cwe-597
type: cwe
description: "The product uses the wrong operator when comparing a string, such as using \"==\" when the .equals() method should be used instead."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-597: Use of Wrong Operator in String Comparison

- **Abstraction:** Variant
- **Structure:** Simple
- **Status:** Draft
- **CVEs with this as primary weakness:** 3
- **Of those, in CISA KEV:** 0
- **New in 2026:** 0
- **CWE catalog version:** 4.21

## Description

The product uses the wrong operator when comparing a string, such as using "==" when the .equals() method should be used instead.

## Background

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.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Applicable platforms

**Languages:** Java (Undetermined), JavaScript (Undetermined), PHP (Undetermined)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Other | Varies by Context | 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. |

## Potential mitigations

### 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

## Detection methods

### Automated Static Analysis

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Effectiveness: High

## 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:

```java
String str1 = new String("Hello");
String str2 = new String("Hello");
if (str1 == str2) {
    System.out.println("str1 == str2");
}
```

Good code:

```java
if (str1.equals(str2)) {
    System.out.println("str1 equals 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:

### 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:

```javascript
<p id="ieq3s1" type="text">(i === s1) is FALSE</p>
<p id="s4eq3i" type="text">(s4 === i) is FALSE</p>
<p id="s4eq3s1" type="text">(s4 === s1) is FALSE</p>

var i = 65;
var s1 = '65';
var s4 = new String('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";
}
```

Good code:

```javascript
<p id="ieq2s1" type="text">(i == s1) is FALSE</p>
<p id="s4eq2i" type="text">(s4 == i) is FALSE</p>
<p id="s4eq2s1" type="text">(s4 == s1) is FALSE</p>

var i = 65;
var s1 = '65';
var s4 = new String('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";
}
```

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:

### 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:

```php
var $i = 65;
var $s1 = "65";

if ($i === $s1)
{
    echo '($i === $s1) is TRUE'. "\n";
}
else
{
    echo '($i === $s1) is FALSE'. "\n";
}
```

Good code:

```php
var $i = 65;
var $s1 = "65";

if ($i == $s1)
{
    echo '($i == $s1) is TRUE'. "\n";
}
else
{
    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:

## Real CVEs with this weakness

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2021-4259 | phpRedisAdmin login.inc.php authHttpDigest wrong operator in string comparison | critical | 9.8 | - | No | 2022-12-19 |
| CVE-2021-3797 | Use of Wrong Operator in String Comparison in hestiacp/hestiacp | critical | 9.8 | - | No | 2021-09-15 |
| CVE-2022-36072 | SilverwareGames.io used == for hashing instead of === | medium | 5.9 | - | No | 2022-09-06 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-480](https://radicalnotion.ai/cwe/cwe-480) | Use of Incorrect Operator |
| Parent | [CWE-595](https://radicalnotion.ai/cwe/cwe-595) | Comparison of Object References Instead of Object Contents |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| The CERT Oracle Secure Coding Standard for Java (2011) | Do not use the equality operators when comparing values of boxed primitives | EXP03-J | - |
| The CERT Oracle Secure Coding Standard for Java (2011) | Do not use the equality operators when comparing values of boxed primitives | EXP03-J | - |
| SEI CERT Perl Coding Standard | Use the correct operator type for comparing values | EXP35-PL | CWE More Specific |
| Software Fault Patterns | Glitch in computation | SFP1 | - |

## Frequently asked questions

### What is CWE-597?

The product uses the wrong operator when comparing a string, such as using "==" when the.equals() method should be used instead.

### What CVEs are caused by CWE-597?

3 recorded CVEs are attributed to CWE-597, including CVE-2021-4259, CVE-2021-3797 and CVE-2022-36072.

### How do you prevent CWE-597?

Within Java, use.equals() to compare string values.

### How is CWE-597 detected?

Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

### What are the consequences of CWE-597?

Exploiting CWE-597 can lead to: Varies by Context.

### Is CWE-597 actively exploited?

3 recorded CVEs are caused by CWE-597; none are currently in CISA's KEV catalog of actively exploited flaws.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/597.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-597). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
