Safeguard
Application Security•

CWE-482 Comparing Instead of Assigning in C and C++

A double equals sign where a single one was meant means the assignment never happens. Learn how to spot it and why tooling catches it quickly.

Safeguard Security Team
2 min read

CWE-482, Comparing instead of Assigning, is the mirror image of CWE-481. The code uses a comparison operator when the intent was to assign a value. MITRE lists C and C++ and notes that the two operators look close enough to be confused.

How it happens

A statement meant to set a variable instead tests it and throws the result away.

// Vulnerable: compares, does not assign
int retries;
retries == 3;        /* no effect */

The line compiles in C and C++. retries keeps whatever value it had, which may be uninitialized.

What an attacker gains

MITRE lists availability and integrity impact through an unexpected state. Because the assignment never takes place, it says the problem should cause obvious program execution problems. A security variable that never gets its intended value, such as a flag or a limit, can leave a check running on stale data. The record has no observed CVEs.

How to find it

MITRE says many IDEs and static analysis products detect this, and an IDE or linter can highlight it. Compilers also warn about a statement with no effect. Enable those warnings and fail the build on them.

How to fix it

Replace the comparison with an assignment, then keep the tooling on so the next one is caught. MITRE's listed mitigation is testing with the IDEs and static analysis products that detect the issue.

retries = 3;

Quick check

Why is this usually found quickly?

The variable never changes, so the program misbehaves in plain view. Compiler warnings about statements with no effect catch it even sooner.

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.