1151
Detection Methods
Automated Static Analysis
This weakness can be found easily using static analysis. However in some cases an operator
might appear to be incorrect, but is actually correct and reflects unusual logic within the program.
Manual Static Analysis
This weakness can be found easily using static analysis. However in some cases an operator
might appear to be incorrect, but is actually correct and reflects unusual logic within the program.
Demonstrative Examples
Example 1:
The following C/C++ and C# examples attempt to validate an int input parameter against the
integer value 100.
Example Language: C (Bad)
int isValid(int value) {
if (value=100) {
printf("Value is valid\n");
return(1);
}
printf("Value is not valid\n");
return(0);
}
Example Language: C# (Bad)
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.
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.
Example Language: C (Bad)
#define SIZE 50
int *tos, *p1, stack[SIZE];
void push(int i) {
p1++;
if(p1==(tos+SIZE)) {
// Print stack overflow error message and exit
}
*p1 == i;
}
int pop(void) {
if(p1==tos) {
// Print stack underflow error message and exit
CWE Version 4.14
CWE-480: Use of Incorrect Operator
CWE-480: Use of Incorrect Operator