CWE-481: Assigning instead of Comparing
The code uses an operator for assignment when the intention was to perform a comparison.
Last updated
Overview
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 and 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.
Common consequences
What can happen when CWE-481 is exploited.
Alter Execution Logic
Affects: Other
How it happens
When it is introduced
Typically introduced during these phases of the software lifecycle.
Applies to
Languages
How to prevent it
Practical mitigations for CWE-481, grouped by where in the lifecycle they apply.
Place constants on the left. If one attempts to assign a constant with a variable, the compiler will produce an error.
How to detect it
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 weaknesses.
Code examples
Illustrative examples from MITRE showing how the weakness appears in code.
The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.
Vulnerable example
int isValid(int value) {Vulnerable example
bool isValid(int value) {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.
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.
Vulnerable example
void processString (char *str) {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.
Vulnerable example
public void checkValid(boolean isValid) {Safe example
public void checkValid(boolean isValid) {Safe example
public void checkValid(boolean isValid) {The following example demonstrates the weakness.
Vulnerable example
void called(int foo){Terminology & mappings
Mapped taxonomies
- 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 fit
Frequently asked questions
Common questions about CWE-481.
- 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.
References
- MITRE CWE definition (CWE-481) (opens in a new tab)
- CWE-481 vulnerabilities on NVD (opens in a new tab)
- Learn: What is a CWE?
Weakness data is sourced from the MITRE CWE catalog (v4.20). CVE associations are aggregated and kept current by RadicalNotion.AI.
Stay ahead of CWE-481
Get alerted the moment a new CWE-481 vulnerability affects your stack, with AI-written analysis, severity context, and remediation guidance.