The code uses an operator for assignment when the intention was to perform a comparison.
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.

Primary

Implementation

Low

Other
Alter Execution Logic

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.)
High

Testing
Many IDEs and static analysis products will detect this problem.

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

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

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

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.

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

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();}}}
}

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.

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;}}

While most Java compilers will catch the use of an assignment operator when a comparison operator is required, for boolean variables in Java the use of the assignment operator within an expression is allowed. If possible, try to avoid using comparison operators on boolean variables in java. Instead, let the values of the variables stand for themselves, as in the following code.

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

Alternatively, to test for false, just use the boolean NOT operator.

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

The following example demonstrates the weakness.

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

called(2);return 0;
}

Assigning instead of comparing

SFP1
Glitch in computation

EXP45-C
Do not perform assignments in selection statements
CWE More Abstract

Allowed
This CWE entry is at the Variant level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.
Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.

CLASP
2006-07-19
Draft 3
2006-07-19

Eric Dalci
Cigital
2008-07-01
updated Time_of_Introduction

CWE Content Team
MITRE
2008-09-08
updated Applicable_Platforms, Description, Relationships, Other_Notes, Taxonomy_Mappings

CWE Content Team
MITRE
2009-05-27
updated Demonstrative_Examples

CWE Content Team
MITRE
2009-07-27
updated Description, Other_Notes

CWE Content Team
MITRE
2011-06-01
updated Common_Consequences

CWE Content Team
MITRE
2012-05-11
updated References, Relationships

CWE Content Team
MITRE
2012-10-30
updated Demonstrative_Examples, Potential_Mitigations

CWE Content Team
MITRE
2014-07-30
updated Relationships, Taxonomy_Mappings

CWE Content Team
MITRE
2017-01-19
updated Relationships

CWE Content Team
MITRE
2017-11-08
updated Demonstrative_Examples, Taxonomy_Mappings

CWE Content Team
MITRE
2019-01-03
updated Relationships

CWE Content Team
MITRE
2020-02-24
updated References, Relationships

CWE Content Team
MITRE
2021-03-15
updated Demonstrative_Examples, Potential_Mitigations

CWE Content Team
MITRE
2023-04-27
updated Detection_Factors, Relationships

CWE Content Team
MITRE
2023-06-29
updated Mapping_Notes

CWE Content Team
MITRE
2025-12-11
4.19
2025-12-11
updated Weakness_Ordinalities