CWE-481: Assigning instead of Comparing
Description
The code uses an operator for assignment when the intention was to perform a comparison.
Submission Date :
July 19, 2006, midnight
Modification Date :
2023-06-29 00:00:00+00:00
Organization :
MITRE
Extended Description
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.
Example - 1
The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100. 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.
printf("Value is valid\n");return(1);
if (value=100) {}printf("Value is not valid\n");return(0);int isValid(int value) {}
Console.WriteLine("Value is valid.");return true;
if (value=100) {}Console.WriteLine("Value is not valid.");return false;bool isValid(int value) {}
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..
processChar(str[i]);
movingToNewInput();}if (isalnum(str[i])){}else if (str[i] = ':') {}int i;for(i=0; i<strlen(str); i++) {}void processString (char *str) {}
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.
System.out.println("Performing processing");doSomethingImportant();
System.out.println("Not Valid, do not perform processing");return;if (isValid = true) {}else {}public void checkValid(boolean isValid) {}
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.
System.out.println("Performing processing");doSomethingImportant();
System.out.println("Not Valid, do not perform processing");return;if (isValid) {}else {}public void checkValid(boolean isValid) {}
Alternatively, to test for false, just use the boolean NOT operator.
System.out.println("Not Valid, do not perform processing");return;
if (!isValid) {}System.out.println("Performing processing");doSomethingImportant();public void checkValid(boolean isValid) {}
Example - 4
The following example demonstrates the weakness.
if (foo=1) printf("foo\n");
called(2);return 0;void called(int foo){}int main() {}
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
Visit http://cwe.mitre.org/ for more details.