Description
The code uses an operator for assignment when the intention was to perform a comparison.
In many languages the compare statement is very close in appearance to the assignment statement and are often confused. This bug is generally the result of a typo and usually causes obvious problems with program execution. If the comparison is in an if statement, the if statement will usually evaluate the value of the right-hand side of the predicate.
Potential Impact
Other
Alter Execution Logic
Demonstrative Examples
int isValid(int value) {if (value=100) {printf("Value is valid\n");return(1);}printf("Value is not valid\n");return(0);}bool isValid(int value) {if (value=100) {Console.WriteLine("Value is valid.");return true;}Console.WriteLine("Value is not valid.");return false;}void processString (char *str) {
int i;
for(i=0; i<strlen(str); i++) {if (isalnum(str[i])){processChar(str[i]);}else if (str[i] = ':') {movingToNewInput();}}}
}public void checkValid(boolean isValid) {if (isValid = true) {System.out.println("Performing processing");doSomethingImportant();}else {System.out.println("Not Valid, do not perform processing");return;}}public void checkValid(boolean isValid) {if (isValid) {System.out.println("Performing processing");doSomethingImportant();}else {System.out.println("Not Valid, do not perform processing");return;}}public void checkValid(boolean isValid) {if (!isValid) {System.out.println("Not Valid, do not perform processing");return;}System.out.println("Performing processing");doSomethingImportant();}void called(int foo){if (foo=1) printf("foo\n");}int main() {
called(2);return 0;
}Mitigations & Prevention
Place constants on the left. If one attempts to assign a constant with a variable, the compiler will produce an error.
Detection Methods
- Automated Static Analysis High — 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 sea
- Automated Static Analysis - Source Code — An Integrated Development Environment (IDE) or linter can report or highlight this weaknesses.
Related Weaknesses
Taxonomy Mappings
- CLASP: — Assigning instead of comparing
- Software Fault Patterns: SFP1 — Glitch in computation
- CERT C Secure Coding: EXP45-C — Do not perform assignments in selection statements
Frequently Asked Questions
What is CWE-481?
CWE-481 (Assigning instead of Comparing) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. The code uses an operator for assignment when the intention was to perform a comparison.
How can CWE-481 be exploited?
Attackers can exploit CWE-481 (Assigning instead of Comparing) to alter execution logic. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-481?
Key mitigations include: Place constants on the left. If one attempts to assign a constant with a variable, the compiler will produce an error.
What is the severity of CWE-481?
CWE-481 is classified as a Variant-level weakness (Low-Medium abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.