{"@ID": "481", "@Name": "Assigning instead of Comparing", "@Abstraction": "Variant", "@Structure": "Simple", "@Status": "Draft", "Description": "The code uses an operator for assignment when the intention was to perform a comparison.", "Dimensions": {"Dimension": [{"@Type": "Behavior-Type", "#text": "Assigning"}, {"@Type": "Behavior-Property", "#text": "instead of Comparing"}]}, "Related_Weaknesses": {"Related_Weakness": [{"@Nature": "ChildOf", "@CWE_ID": "480", "@View_ID": "1000", "@Ordinal": "Primary"}, {"@Nature": "CanPrecede", "@CWE_ID": "697", "@View_ID": "1000"}]}, "Weakness_Ordinalities": {"Weakness_Ordinality": {"Ordinality": "Primary"}}, "Applicable_Platforms": {"Language": [{"@Name": "C", "@Prevalence": "Undetermined"}, {"@Name": "C++", "@Prevalence": "Undetermined"}, {"@Name": "Java", "@Prevalence": "Undetermined"}, {"@Name": "C#", "@Prevalence": "Undetermined"}]}, "Modes_Of_Introduction": {"Introduction": {"Phase": "Implementation", "Note": "In many languages, the compare statement is very\n\t      close in appearance to the assignment statement and are\n\t      often confused. This bug is generally the result of a\n\t      typo. This usually causes obvious problems with program\n\t      execution. If the comparison is in an \"if\" statement,\n\t      the if statement will usually evaluate the value of the\n\t      right-hand side of the predicate. As a result, this\n\t      weakness might only go undetected if there's\n\t      insufficient testing and rarely-reached code\n\t      paths."}}, "Likelihood_Of_Exploit": "Low", "Common_Consequences": {"Consequence": {"Scope": "Other", "Impact": "Alter Execution Logic"}}, "Detection_Methods": {"Detection_Method": [{"@Detection_Method_ID": "DM-14", "Method": "Automated Static Analysis", "Description": "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.)", "Effectiveness": "High"}, {"@Detection_Method_ID": "DM-16", "Method": "Automated Static Analysis - Source Code", "Description": "An Integrated Development Environment (IDE) or linter can report or highlight this weakness."}]}, "Potential_Mitigations": {"Mitigation": {"Phase": "Implementation", "Description": "Place constants on the left. If one attempts to assign a constant with a variable, the compiler will produce an error."}}, "Demonstrative_Examples": {"Demonstrative_Example": [{"@Demonstrative_Example_ID": "DX-140", "Intro_Text": "The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.", "Example_Code": [{"@Nature": "Bad", "@Language": "C", "xhtml:div": {"xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "printf(\"Value is valid\\n\");return(1);"}, "xhtml:br": [null, null], "#text": "if (value=100) {}printf(\"Value is not valid\\n\");return(0);"}, "#text": "int isValid(int value) {}"}, "#text_blocks": ["int isValid(int value) {", "if (value=100) {", "printf(\"Value is valid\\n\");", "return(1);", "}", "printf(\"Value is not valid\\n\");", "return(0);", "}"]}, {"@Nature": "Bad", "@Language": "C#", "xhtml:div": {"xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "Console.WriteLine(\"Value is valid.\");return true;"}, "xhtml:br": [null, null], "#text": "if (value=100) {}Console.WriteLine(\"Value is not valid.\");return false;"}, "#text": "bool isValid(int value) {}"}, "#text_blocks": ["bool isValid(int value) {", "if (value=100) {", "Console.WriteLine(\"Value is valid.\");", "return true;", "}", "Console.WriteLine(\"Value is not valid.\");", "return false;", "}"]}], "Body_Text": "However, the expression to be evaluated in the if statement uses the assignment operator \"=\" rather than the comparison operator \"==\". The result of using the assignment operator instead of the comparison operator causes the int variable to be reassigned locally and the expression in the if statement will always evaluate to the value on the right hand side of the expression. This will result in the input value not being properly validated, which can cause unexpected results."}, {"Intro_Text": "In this example, we show how assigning instead of comparing can impact code when values are being passed by reference instead of by value. Consider a scenario in which a string is being processed from user input. Assume the string has already been formatted such that different user inputs are concatenated with the colon character. When the processString function is called, the test for the colon character will result in an insertion of the colon character instead, adding new input separators. Since the string was passed by reference, the data sentinels will be inserted in the original string (CWE-464), and further processing of the inputs will be altered, possibly malformed.", "Example_Code": {"@Nature": "Bad", "@Language": "C", "xhtml:div": {"xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:br": [null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": [{"@style": "margin-left:1em;", "#text": "processChar(str[i]);"}, {"@style": "margin-left:1em;", "#text": "movingToNewInput();}"}], "xhtml:br": null, "#text": "if (isalnum(str[i])){}else if (str[i] = ':') {}"}, "#text": "int i;\n                           for(i=0; i<strlen(str); i++) {}"}}, "#text": "void processString (char *str) {}"}, "#text_blocks": ["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();}", "}", "}", "}"]}}, {"Intro_Text": "The following Java example attempts to perform some processing based on the boolean value of the input parameter. However, the expression to be evaluated in the if statement uses the assignment operator \"=\" rather than the comparison operator \"==\". As with the previous examples, the variable will be reassigned locally and the expression in the if statement will evaluate to true and unintended processing may occur.", "Example_Code": [{"@Nature": "Bad", "@Language": "Java", "xhtml:div": {"xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "#text": "System.out.println(\"Performing processing\");doSomethingImportant();"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "System.out.println(\"Not Valid, do not perform processing\");return;"}], "xhtml:br": null, "#text": "if (isValid = true) {}else {}"}, "#text": "public void checkValid(boolean isValid) {}"}, "#text_blocks": ["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;", "}", "}"]}, {"@Nature": "Good", "@Language": "Java", "xhtml:div": {"xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "#text": "System.out.println(\"Performing processing\");doSomethingImportant();"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "System.out.println(\"Not Valid, do not perform processing\");return;"}], "xhtml:br": null, "#text": "if (isValid) {}else {}"}, "#text": "public void checkValid(boolean isValid) {}"}, "#text_blocks": ["public void checkValid(boolean isValid) {", "if (isValid) {", "System.out.println(\"Performing processing\");", "doSomethingImportant();", "}", "else {", "System.out.println(\"Not Valid, do not perform processing\");", "return;", "}", "}"]}, {"@Nature": "Good", "@Language": "Java", "xhtml:div": {"xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "System.out.println(\"Not Valid, do not perform processing\");return;"}, "xhtml:br": [null, null], "#text": "if (!isValid) {}System.out.println(\"Performing processing\");doSomethingImportant();"}, "#text": "public void checkValid(boolean isValid) {}"}, "#text_blocks": ["public void checkValid(boolean isValid) {", "if (!isValid) {", "System.out.println(\"Not Valid, do not perform processing\");", "return;", "}", "System.out.println(\"Performing processing\");", "doSomethingImportant();", "}"]}], "Body_Text": ["While most Java compilers will catch the use of an assignment operator when a comparison operator is required, for boolean variables in Java the use of the assignment operator within an expression is allowed. If possible, try to avoid using comparison operators on boolean variables in java. Instead, let the values of the variables stand for themselves, as in the following code.", "Alternatively, to test for false, just use the boolean NOT operator."]}, {"Intro_Text": "The following example demonstrates the weakness.", "Example_Code": {"@Nature": "Bad", "@Language": "C", "xhtml:div": {"xhtml:div": [{"@style": "margin-left:1em;", "#text": "if (foo=1) printf(\"foo\\n\");"}, {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:br": [null, null], "#text": "called(2);return 0;"}}], "xhtml:br": null, "#text": "void called(int foo){}int main() {}"}, "#text_blocks": ["void called(int foo){", "if (foo=1) printf(\"foo\\n\");", "}", "int main() {", "called(2);", "return 0;", "}"]}}]}, "Taxonomy_Mappings": {"Taxonomy_Mapping": [{"@Taxonomy_Name": "CLASP", "Entry_Name": "Assigning instead of comparing"}, {"@Taxonomy_Name": "Software Fault Patterns", "Entry_ID": "SFP1", "Entry_Name": "Glitch in computation"}, {"@Taxonomy_Name": "CERT C Secure Coding", "Entry_ID": "EXP45-C", "Entry_Name": "Do not perform assignments in selection statements", "Mapping_Fit": "CWE More Abstract"}]}, "References": {"Reference": [{"@External_Reference_ID": "REF-18"}, {"@External_Reference_ID": "REF-62", "@Section": "Chapter 6, \"Typos\", Page 289"}]}, "Mapping_Notes": {"Usage": "Allowed", "Rationale": "This CWE entry is at the Variant level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.", "Comments": "Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.", "Reasons": {"Reason": {"@Type": "Acceptable-Use"}}}, "Content_History": {"Submission": {"Submission_Name": "CLASP", "Submission_Date": "2006-07-19", "Submission_Version": "Draft 3", "Submission_ReleaseDate": "2006-07-19"}, "Modification": [{"Modification_Name": "Eric Dalci", "Modification_Organization": "Cigital", "Modification_Date": "2008-07-01", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Time_of_Introduction"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-09-08", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Applicable_Platforms, Description, Relationships, Other_Notes, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-05-27", "Modification_Version": "1.4", "Modification_ReleaseDate": "2009-05-27", "Modification_Comment": "updated Demonstrative_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-07-27", "Modification_Version": "1.5", "Modification_ReleaseDate": "2009-07-27", "Modification_Comment": "updated Description, Other_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-06-01", "Modification_Version": "1.13", "Modification_ReleaseDate": "2011-06-01", "Modification_Comment": "updated Common_Consequences"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2012-05-11", "Modification_Version": "2.2", "Modification_ReleaseDate": "2012-05-15", "Modification_Comment": "updated References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2012-10-30", "Modification_Version": "2.3", "Modification_ReleaseDate": "2012-10-30", "Modification_Comment": "updated Demonstrative_Examples, Potential_Mitigations"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2014-07-30", "Modification_Version": "2.8", "Modification_ReleaseDate": "2014-07-31", "Modification_Comment": "updated Relationships, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2017-01-19", "Modification_Version": "2.10", "Modification_ReleaseDate": "2017-01-19", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2017-11-08", "Modification_Version": "3.0", "Modification_ReleaseDate": "2017-11-08", "Modification_Comment": "updated Demonstrative_Examples, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2019-01-03", "Modification_Version": "3.2", "Modification_ReleaseDate": "2019-01-03", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-02-24", "Modification_Version": "4.0", "Modification_ReleaseDate": "2020-02-24", "Modification_Comment": "updated References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2021-03-15", "Modification_Version": "4.4", "Modification_ReleaseDate": "2021-03-15", "Modification_Comment": "updated Demonstrative_Examples, Potential_Mitigations"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-04-27", "Modification_Version": "4.11", "Modification_ReleaseDate": "2023-04-27", "Modification_Comment": "updated Detection_Factors, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-06-29", "Modification_Version": "4.12", "Modification_ReleaseDate": "2023-06-29", "Modification_Comment": "updated Mapping_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-12-11", "Modification_Version": "4.19", "Modification_ReleaseDate": "2025-12-11", "Modification_Comment": "updated Weakness_Ordinalities"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2026-04-30", "Modification_Version": "4.20", "Modification_ReleaseDate": "2026-04-30", "Modification_Comment": "updated Detection_Factors, Potential_Mitigations"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2026-10-28", "Modification_Version": "5.0", "Modification_ReleaseDate": "2026-10-28", "Modification_Comment": "updated Description, Detection_Factors, Dimensions, Modes_of_Introduction"}]}, "entry_type": "Weakness"}
