Comparing instead of Assigning

Draft Variant
Structure: Simple
Description

This vulnerability occurs when a developer accidentally uses a comparison operator (like '==') where an assignment operator (like '=') was intended, creating a logic error instead of setting a value.

Extended Description

This common coding mistake happens because assignment and comparison operators look very similar in many programming languages. For example, using `if (x = 5)` instead of `if (x == 5)` assigns the value 5 to `x` and then evaluates the assignment's result as the condition, which is often always true, breaking the intended program logic. To prevent this, developers should adopt defensive coding habits like placing constants on the left side in comparisons (e.g., `if (5 == x)`), which would cause a compilation error if written incorrectly as `if (5 = x)`. Modern compilers and linters often flag this pattern with warnings like "possible unintended assignment," which should always be investigated and corrected.

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