CWE-482: Comparing instead of Assigning
Description
The code uses an operator for comparison when the intention was to perform an assignment.
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; they are often confused.
Example - 1
The following example demonstrates the weakness.
foo==1;if (foo==1) System.out.println("foo\n");
called(2);return 0;void called(int foo) {}int main() {}
Example - 2
The following C/C++ example shows a simple implementation of a stack that includes methods for adding and removing integer values from the stack. The example uses pointers to add and remove integer values to the stack array variable. The push method includes an expression to assign the integer value to the location in the stack pointed to by the pointer variable. However, this expression uses the comparison operator "==" rather than the assignment operator "=". The result of using the comparison operator instead of the assignment operator causes erroneous values to be entered into the stack and can cause unexpected results.
// // Print stack overflow error message and exit//
p1++;if(p1==(tos+SIZE)) {}*p1 == i;
// // Print stack underflow error message and exit//
if(p1==tos) {}p1--;return *(p1+1);
// // initialize tos and p1 to point to the top of stack//
// // code to add and remove items from stack//
tos = stack;p1 = stack;...return 0;#define SIZE 50int *tos, *p1, stack[SIZE];void push(int i) {}int pop(void) {}int main(int argc, char *argv[]) {}
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.