Comparing instead of Assigning

Draft Variant
Structure: Simple
Description

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

Extended Description

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

Common Consequences 1
Scope: AvailabilityIntegrity

Impact: Unexpected State

The assignment will not take place, which should cause obvious program execution problems.

Detection Methods 1
Automated Static AnalysisHigh
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.)
Potential Mitigations 1
Phase: Testing
Many IDEs and static analysis products will detect this problem.
Demonstrative Examples 2
The following example demonstrates the weakness.

Code Example:

Bad
Java
java

ID : DX-103

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.

Code Example:

Bad
C
c

// Print stack overflow error message and exit* } *p1 == i;}

c
c

// initialize tos and p1 to point to the top of stack* tos = stack; p1 = stack;

c
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.
References 2
The CLASP Application Security Process
Secure Software, Inc.
2005
ID: REF-18
The Art of Software Security Assessment
Mark Dowd, John McDonald, and Justin Schuh
Addison Wesley
2006
ID: REF-62
Likelihood of Exploit

Low

Applicable Platforms
Languages:
C : UndeterminedC++ : Undetermined
Modes of Introduction
Implementation
Related Weaknesses
Taxonomy Mapping
  • CLASP
  • Software Fault Patterns