CWEweakness
CWE-595 Comparison of Object References Instead of Object Contents
- Abstraction
- Variant
- Structure
- Simple
- Status
- Incomplete
- Languages
- Java, JavaScript, Not Language-Specific, PHP, Python
- CWE version
- 5.0
Description
The product compares object references instead of the contents of the objects themselves, preventing it from detecting equivalent objects.
Extended description
For example, in Java, comparing objects using == usually produces deceptive results, since the == operator compares object references rather than values; often, this means that using == for strings is actually comparing the strings' references, not their values.
Common consequences
• Other: Varies by Context — This weakness can lead to erroneous results that can cause unexpected application behaviors.
Potential mitigations
• [Implementation] In Java, use the equals() method to compare objects instead of the == operator. If using ==, it is important for performance reasons that your objects are created by a static factory, not by a constructor.
Detection methods
• Automated Static Analysis: 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.)
Related
Child of · CWE (1)
- CWE-1025 Comparison Using Wrong Factors weakness
Referenced by CWE · child of (2)
Referenced by CWE · has member (1)
- CWE-884 CWE Cross-section view