Safeguard
Application Security•

CWE-481 Assigning Instead of Comparing in Conditions

Writing a single equals sign where two were meant changes a value and makes the test always true. See the pattern and the easy guards against it.

Safeguard Security Team
2 min read

CWE-481, Assigning instead of Comparing, is the classic one equals sign bug. The code uses an assignment operator where a comparison was intended. MITRE lists C, C++, Java and C#, and describes it as a child of CWE-480, Use of Incorrect Operator.

How it happens

In many languages the compare and assign operators look almost the same, so they get confused. MITRE says the bug is generally a typo and usually causes obvious problems in program execution.

// Vulnerable: assigns 100 to value, then tests the result
int is_valid(int value) {
    if (value = 100) {
        return 1;
    }
    return 0;
}

The assignment overwrites value and evaluates to 100, which is true. The function now returns 1 for every input. When the comparison sits in an if, the statement generally evaluates the right hand side of the assignment as the condition.

What an attacker gains

The record lists altered execution logic. A check that always passes, or a variable silently overwritten, can defeat a validation step. The record has no observed CVEs for this entry.

How to find it

Static analysis finds many cases, and MITRE adds that an IDE or linter can report or highlight the weakness. Compilers also warn about an assignment used as a condition. Treat that warning as an error.

How to fix it

MITRE suggests placing constants on the left. If you accidentally write one equals sign, assigning to a constant is a compile error.

if (100 == value) {     /* typo "100 = value" will not compile */
    return 1;
}

Combine that with compiler warnings and a linter in your build.

Quick check

Why does the broken check pass every input?

An assignment evaluates to the value assigned. Assigning 100 yields 100, which counts as true.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.