Comparing instead of Assigning
Weakness ID: 482 (Weakness Variant)Status: Draft
+ Description

Description Summary

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.

+ Time of Introduction
  • Implementation
+ Applicable Platforms

Languages

C

C++

+ Modes of Introduction

This bug primarily originates from a typo.

+ Common Consequences
ScopeEffect
Availability

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

+ Likelihood of Exploit

Low

+ Demonstrative Examples

Example 1

(Bad Code)
Example Languages: C and C++ and Java 
void called(int foo) {
foo==1;
if (foo==1) printf("foo\n");
}
int main() {

called(2);
return 0;
}
+ Potential Mitigations

Pre-design: Through Build: Many IDEs and static analysis products will detect this problem.

+ Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfWeakness BaseWeakness Base480Use of Incorrect Operator
Development Concepts699
Research Concepts (primary)1000
ChildOfCategoryCategory569Expression Issues
Development Concepts (primary)699
ChildOfCategoryCategory747CERT C Secure Coding Section 49 - Miscellaneous (MSC)
Weaknesses Addressed by the CERT C Secure Coding Standard (primary)734
+ Taxonomy Mappings
Mapped Taxonomy NameNode IDFitMapped Node Name
CLASPComparing instead of assigning
CERT C Secure CodingMSC02-CAvoid errors of omission
+ Content History
Submissions
Submission DateSubmitterOrganizationSource
CLASPExternally Mined
Modifications
Modification DateModifierOrganizationSource
2008-07-01Eric DalciCigitalExternal
updated Time of Introduction
2008-09-08CWE Content TeamMITREInternal
updated Applicable Platforms, Description, Relationships, Other Notes, Taxonomy Mappings
2008-11-24CWE Content TeamMITREInternal
updated Relationships, Taxonomy Mappings
2009-07-27CWE Content TeamMITREInternal
updated Common Consequences, Modes of Introduction
2009-10-29CWE Content TeamMITREInternal
updated Other Notes