---
title: "CWE-481: Assigning instead of Comparing"
url: https://radicalnotion.ai/cwe/cwe-481
type: cwe
description: "The code uses an operator for assignment when the intention was to perform a comparison."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-481: Assigning instead of Comparing

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

## Description

The code uses an operator for assignment when the intention was to perform a comparison.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | In many languages, the compare statement is very close in appearance to the assignment statement and are often confused. This bug is generally the result of a typo. This usually causes obvious problems with program execution. If the comparison is in an "if" statement, the if statement will usually evaluate the value of the right-hand side of the predicate. As a result, this weakness might only go undetected if there's insufficient testing and rarely-reached code paths. |

## Applicable platforms

**Languages:** C (Undetermined), C++ (Undetermined), Java (Undetermined), C# (Undetermined)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Other | Alter Execution Logic | - |

## Potential mitigations

### Implementation

Place constants on the left. If one attempts to assign a constant with a variable, the compiler will produce an error.

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

### Automated Static Analysis - Source Code

An Integrated Development Environment (IDE) or linter can report or highlight this weakness.

## Demonstrative examples

### Example 1

The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.

Bad code:

```c
int isValid(int value) {
    if (value=100) {
        printf("Value is valid\n");
        return(1);
    }
    printf("Value is not valid\n");
    return(0);
}
```

Bad code:

```c#
bool isValid(int value) {
    if (value=100) {
        Console.WriteLine("Value is valid.");
        return true;
    }
    Console.WriteLine("Value is not valid.");
    return false;
}
```

However, the expression to be evaluated in the if statement uses the assignment operator "=" rather than the comparison operator "==". The result of using the assignment operator instead of the comparison operator causes the int variable to be reassigned locally and the expression in the if statement will always evaluate to the value on the right hand side of the expression. This will result in the input value not being properly validated, which can cause unexpected results.

### Example 2

In this example, we show how assigning instead of comparing can impact code when values are being passed by reference instead of by value. Consider a scenario in which a string is being processed from user input. Assume the string has already been formatted such that different user inputs are concatenated with the colon character. When the processString function is called, the test for the colon character will result in an insertion of the colon character instead, adding new input separators. Since the string was passed by reference, the data sentinels will be inserted in the original string (CWE-464), and further processing of the inputs will be altered, possibly malformed.

Bad code:

```c
void processString (char *str) {
    int i;

    for(i=0; i<strlen(str); i++) {
        if (isalnum(str[i])){
            processChar(str[i]);
        }
        else if (str[i] = ':') {
            movingToNewInput();}
        }
    }
}
```

### Example 3

The following Java example attempts to perform some processing based on the boolean value of the input parameter. However, the expression to be evaluated in the if statement uses the assignment operator "=" rather than the comparison operator "==". As with the previous examples, the variable will be reassigned locally and the expression in the if statement will evaluate to true and unintended processing may occur.

Bad code:

```java
public void checkValid(boolean isValid) {
    if (isValid = true) {
        System.out.println("Performing processing");
        doSomethingImportant();
    }
    else {
        System.out.println("Not Valid, do not perform processing");
        return;
    }
}
```

Good code:

```java
public void checkValid(boolean isValid) {
    if (isValid) {
        System.out.println("Performing processing");
        doSomethingImportant();
    }
    else {
        System.out.println("Not Valid, do not perform processing");
        return;
    }
}
```

Good code:

```java
public void checkValid(boolean isValid) {
    if (!isValid) {
        System.out.println("Not Valid, do not perform processing");
        return;
    }
    System.out.println("Performing processing");
    doSomethingImportant();
}
```

### Example 4

The following example demonstrates the weakness.

Bad code:

```c
void called(int foo){
    if (foo=1) printf("foo\n");
}
int main() {

    called(2);
    return 0;
}
```

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-480](https://radicalnotion.ai/cwe/cwe-480) | Use of Incorrect Operator |
| Can precede | [CWE-697](https://radicalnotion.ai/cwe/cwe-697) | Incorrect Comparison |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| CLASP | Assigning instead of comparing | - | - |
| Software Fault Patterns | Glitch in computation | SFP1 | - |
| CERT C Secure Coding | Do not perform assignments in selection statements | EXP45-C | CWE More Abstract |

## Frequently asked questions

### What is CWE-481?

The code uses an operator for assignment when the intention was to perform a comparison.

### How do you prevent CWE-481?

Place constants on the left. If one attempts to assign a constant with a variable, the compiler will produce an error.

### How is CWE-481 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-481?

Exploiting CWE-481 can lead to: Alter Execution Logic.

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

---

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