Comparing instead of Assigning

The code uses an operator for comparison when the intention was to perform an assignment.


Description

In many languages, the compare statement is very close in appearance to the assignment statement; they are often confused.

Demonstrations

The following examples help to illustrate the nature of this weakness and describe methods or techniques which can be used to mitigate the risk.

Note that the examples here are by no means exhaustive and any given weakness may have many subtle varieties, each of which may require different detection methods or runtime controls.

Example One

The following example demonstrates the weakness.

void called(int foo) {
  foo==1;
  if (foo==1) System.out.println("foo\n");
}
int main() {


  called(2);
  return 0;

}

Example Two

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.

#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


  }
  p1--;
  return *(p1+1);

}

int main(int argc, char *argv[]) {


  // initialize tos and p1 to point to the top of stack
  tos = stack;
  p1 = stack;
  // code to add and remove items from stack
  ...
  return 0;

}

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.

See Also

Comprehensive Categorization: Insufficient Control Flow Management

Weaknesses in this category are related to insufficient control flow management.

SFP Primary Cluster: Unused entities

This category identifies Software Fault Patterns (SFPs) within the Unused entities cluster (SFP2).

CERT C++ Secure Coding Section 49 - Miscellaneous (MSC)

Weaknesses in this category are related to rules in the Miscellaneous (MSC) section of the CERT C++ Secure Coding Standard. Since not all rules map to specific weaknes...

Comprehensive CWE Dictionary

This view (slice) covers all the elements in CWE.

Weaknesses Introduced During Implementation

This view (slice) lists weaknesses that can be introduced during implementation.

Weaknesses in Software Written in C++

This view (slice) covers issues that are found in C++ programs that are not common to all languages.


Common Weakness Enumeration content on this website is copyright of The MITRE Corporation unless otherwise specified. Use of the Common Weakness Enumeration and the associated references on this website are subject to the Terms of Use as specified by The MITRE Corporation.