{"@ID": "478", "@Name": "Missing Default Case in Multiple Condition Expression", "@Abstraction": "Base", "@Structure": "Simple", "@Status": "Draft", "Description": "The code does not have a default case in an expression with multiple conditions, such as a switch statement.", "Extended_Description": "If a multiple-condition expression (such as a switch in C) omits the default case but does not consider or handle all possible values that could occur, then this might lead to complex logical errors and resultant weaknesses. Because of this, further decisions are made based on poor information, and cascading failure results. This cascading failure may result in any number of security issues, and constitutes a significant failure in the system.", "Related_Weaknesses": {"Related_Weakness": {"@Nature": "ChildOf", "@CWE_ID": "1023", "@View_ID": "1000", "@Ordinal": "Primary"}}, "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"}, {"@Name": "Python", "@Prevalence": "Undetermined"}, {"@Name": "JavaScript", "@Prevalence": "Undetermined"}]}, "Modes_Of_Introduction": {"Introduction": {"Phase": "Implementation"}}, "Common_Consequences": {"Consequence": {"Scope": "Integrity", "Impact": ["Varies by Context", "Alter Execution Logic"], "Note": "Depending on the logical circumstances involved, any consequences may result: e.g., issues of confidentiality, authentication, authorization, availability, integrity, accountability, or non-repudiation."}}, "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"}}, "Potential_Mitigations": {"Mitigation": {"Phase": "Implementation", "Description": "Ensure that there are no cases unaccounted for when adjusting program flow or values based on the value of a given variable. In the case of switch style statements, the very simple act of creating a default case can, if done correctly, mitigate this situation. Often however, the default case is used simply to represent an assumed option, as opposed to working as a check for invalid input. This is poor practice and in some cases is as bad as omitting a default case entirely."}}, "Demonstrative_Examples": {"Demonstrative_Example": [{"Intro_Text": "The following does not properly check the return code in the case where the security_check function returns a -1 value when an error occurs. If an attacker can supply data that will invoke an error, the attacker can bypass the security check:", "Example_Code": [{"@Nature": "Bad", "@Language": "C", "xhtml:div": {"xhtml:br": [null, null, null, null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": [null, null, null], "xhtml:i": "//Break never reached because of exit()", "#text": "printf(\"Security check failed!\\n\");exit(-1);\n                              \n                              break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "printf(\"Security check passed.\\n\");break;"}], "xhtml:br": null, "#text": "case FAILED:\n                           case PASSED:"}}, "xhtml:i": "// program execution continues...", "#text": "#define FAILED 0#define PASSED 1int result;...result = security_check(data);switch (result) {}\n                     \n                     ..."}}, {"@Nature": "Good", "@Language": "C", "xhtml:div": {"xhtml:br": [null, null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": [null, null, null], "xhtml:i": "//Break never reached because of exit()", "#text": "printf(\"Security check failed!\\n\");exit(-1);\n                              \n                              break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "printf(\"Security check passed.\\n\");break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "printf(\"Unknown error (%d), exiting...\\n\",result);exit(-1);"}], "xhtml:br": [null, null], "#text": "case FAILED:\n                           case PASSED:\n                           default:"}}, "#text": "#define FAILED 0#define PASSED 1int result;...result = security_check(data);switch (result) {}"}}], "Body_Text": ["Instead a default label should be used for unaccounted conditions:", "This label is used because the assumption cannot be made that all possible cases are accounted for. A good practice is to reserve the default case for error handling."]}, {"Intro_Text": "In the following Java example the method getInterestRate retrieves the interest rate for the number of points for a mortgage. The number of points is provided within the input parameter and a switch statement will set the interest rate value to be returned based on the number of points.", "Example_Code": [{"@Nature": "Bad", "@Language": "Java", "xhtml:div": {"xhtml:br": [null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:br": [null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "#text": "result = new BigDecimal(INTEREST_RATE_AT_ZERO_POINTS);break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "result = new BigDecimal(INTEREST_RATE_AT_ONE_POINTS);break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "result = new BigDecimal(INTEREST_RATE_AT_TWO_POINTS);break;"}], "xhtml:br": [null, null], "#text": "case 0:\n                                 case 1:\n                                 case 2:"}}, "#text": "BigDecimal result = new BigDecimal(INTEREST_RATE_AT_ZERO_POINTS);\n                           switch (points) {}return result;"}}, "#text": "public static final String INTEREST_RATE_AT_ZERO_POINTS = \"5.00\";public static final String INTEREST_RATE_AT_ONE_POINTS = \"4.75\";public static final String INTEREST_RATE_AT_TWO_POINTS = \"4.50\";...public BigDecimal getInterestRate(int points) {}"}}, {"@Nature": "Good", "@Language": "Java", "xhtml:div": {"xhtml:br": [null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:br": [null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "#text": "result = new BigDecimal(INTEREST_RATE_AT_ZERO_POINTS);break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "result = new BigDecimal(INTEREST_RATE_AT_ONE_POINTS);break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "result = new BigDecimal(INTEREST_RATE_AT_TWO_POINTS);break;"}, {"@style": "margin-left:1em;", "xhtml:br": [null, null], "#text": "System.err.println(\"Invalid value for points, must be 0, 1 or 2\");System.err.println(\"Returning null value for interest rate\");result = null;"}], "xhtml:br": [null, null, null], "#text": "case 0:\n                                 case 1:\n                                 case 2:\n                                 default:"}}, "#text": "BigDecimal result = new BigDecimal(INTEREST_RATE_AT_ZERO_POINTS);\n                           switch (points) {}\n                           return result;"}}, "#text": "public static final String INTEREST_RATE_AT_ZERO_POINTS = \"5.00\";public static final String INTEREST_RATE_AT_ONE_POINTS = \"4.75\";public static final String INTEREST_RATE_AT_TWO_POINTS = \"4.50\";...public BigDecimal getInterestRate(int points) {}"}}], "Body_Text": "However, this code assumes that the value of the points input parameter will always be 0, 1 or 2 and does not check for other incorrect values passed to the method. This can be easily accomplished by providing a default label in the switch statement that outputs an error message indicating an invalid value for the points input parameter and returning a null value."}, {"Intro_Text": "In the following Python example the match-case statements (available in Python version 3.10 and later) perform actions based on the result of the process_data() function. The expected return is either 0 or 1. However, if an unexpected result (e.g., -1 or 2) is obtained then no actions will be taken potentially leading to an unexpected program state.", "Example_Code": [{"@Nature": "Bad", "@Language": "Python", "xhtml:div": {"xhtml:br": [null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": [{"@style": "margin-left:1em;", "#text": "print(\"Properly handle zero case.\")"}, {"@style": "margin-left:1em;", "#text": "print(\"Properly handle one case.\")"}], "xhtml:br": null, "#text": "case 0:\n                     \n                  case 1:"}, "#text": "result = process_data(data)\n                  match result:\n                  \n                  # program execution continues..."}}, {"@Nature": "Good", "@Language": "Python", "xhtml:div": {"xhtml:br": [null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": [{"@style": "margin-left:1em;", "#text": "print(\"Properly handle zero case.\")"}, {"@style": "margin-left:1em;", "#text": "print(\"Properly handle one case.\")"}, {"@style": "margin-left:1em;", "#text": "print(\"Properly handle unexpected condition.\")"}], "xhtml:br": [null, null], "#text": "case 0:\n                     \n                  case 1:\n                     \n                  case _:"}, "#text": "result = process_data(data)\n                  match result:\n                  \n                  # program execution continues..."}}], "Body_Text": "The recommended approach is to add a default case that captures any unexpected result conditions, regardless of how improbable these unexpected conditions might be, and properly handles them."}, {"Intro_Text": "In the following JavaScript example the switch-case statements (available in JavaScript version 1.2 and later) are used to process a given step based on the result of a calcuation involving two inputs. The expected return is either 1, 2, or 3. However, if an unexpected result (e.g., 4) is obtained then no action will be taken potentially leading to an unexpected program state.", "Example_Code": [{"@Nature": "Bad", "@Language": "JavaScript", "xhtml:div": {"xhtml:br": [null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "#text": "alert(\"Process step 1.\");\n                     break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "alert(\"Process step 2.\");\n                     break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "alert(\"Process step 3.\");\n                     break;"}], "xhtml:br": [null, null], "#text": "case 1:\n                     \n                  case 2:\n                     \n                  case 3:"}, "#text": "let step = input1 + input2;\n                  switch(step) {\n                  \n                  }\n                  // program execution continues..."}}, {"@Nature": "Good", "@Language": "JavaScript", "xhtml:div": {"xhtml:br": [null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "#text": "alert(\"Process step 1.\");\n                     break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "alert(\"Process step 2.\");\n                     break;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "alert(\"Process step 3.\");\n                     break;"}, {"@style": "margin-left:1em;", "#text": "alert(\"Unexpected step encountered.\");"}], "xhtml:br": [null, null, null], "#text": "case 1:\n                     \n                  case 2:\n                     \n                  case 3:\n                     \n                  default:"}, "#text": "let step = input1 + input2;\n                  switch(step) {\n                  \n                  }\n                  // program execution continues..."}}], "Body_Text": "The recommended approach is to add a default case that captures any unexpected result conditions and properly handles them."}, {"@Demonstrative_Example_ID": "DX-152", "Intro_Text": "The Finite State Machine (FSM) shown in the \"bad\" code snippet below assigns the output (\"out\") based on the value of state, which is determined based on the user provided input (\"user_input\").", "Example_Code": [{"@Nature": "Bad", "@Language": "Verilog", "xhtml:div": {"xhtml:br": [null, null, null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": [null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": [null, null], "xhtml:div": [{"@style": "margin-left:1em;", "#text": "state = 3'h0;"}, {"@style": "margin-left:1em;", "xhtml:br": [null, null, null, null, null, null], "#text": "3'h0:\n\t\t\t\t\t3'h1:\n\t\t\t\t\t3'h2:\n\t\t\t\t\t3'h3: state = 2'h3;\n\t\t\t\t\t3'h4: state = 2'h2;\n\t\t\t\t\t3'h5: state = 2'h1;"}], "#text": "if (!rst_n)\n\t\t\t\t  \n                  else\n\t\t\t\t  case (user_input)\n                  \n                endcase"}, "#text": "begin\n                \n                \n                end\n                out <= {1'h1, state};"}, "#text": "module fsm_1(out, user_input, clk, rst_n);\n                input [2:0] user_input; \n                input clk, rst_n;\n                output reg [2:0] out;\n                reg [1:0] state;\n                always @ (posedge clk or negedge rst_n )\n                \n                \n                endmodule"}}, {"@Nature": "Good", "@Language": "Verilog", "xhtml:div": {"xhtml:br": null, "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": [null, null, null, null, null, null], "xhtml:b": "default: state = 2'h0;", "#text": "3'h0:\n                3'h1:\n                3'h2:\n                3'h3: state = 2'h3;\n                3'h4: state = 2'h2;\n                3'h5: state = 2'h1;"}, "#text": "case (user_input)\n                \n                endcase"}}], "Body_Text": {"xhtml:p": ["The case statement does not include a default to handle the scenario when the user provides inputs of 3'h6 and 3'h7.  Those inputs push the system to an undefined state and might cause a crash (denial of service) or any other unanticipated outcome.", "Adding a default statement to handle undefined inputs mitigates this issue.  This is shown in the \"Good\" code snippet below.  The default statement is in bold."]}}]}, "Taxonomy_Mappings": {"Taxonomy_Mapping": [{"@Taxonomy_Name": "CLASP", "Entry_Name": "Failure to account for default case in switch"}, {"@Taxonomy_Name": "Software Fault Patterns", "Entry_ID": "SFP4", "Entry_Name": "Unchecked Status Condition"}]}, "References": {"Reference": [{"@External_Reference_ID": "REF-18"}, {"@External_Reference_ID": "REF-62", "@Section": "Chapter 7, \"Switch Statements\", Page 337"}]}, "Mapping_Notes": {"Usage": "Allowed", "Rationale": "This CWE entry is at the Base 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, Common_Consequences, Description, Relationships, Other_Notes, Taxonomy_Mappings, Weakness_Ordinalities"}, {"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 Description, Name"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2010-06-21", "Modification_Version": "1.9", "Modification_ReleaseDate": "2010-06-21", "Modification_Comment": "updated Demonstrative_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-03-29", "Modification_Version": "1.12", "Modification_ReleaseDate": "2011-03-30", "Modification_Comment": "updated Demonstrative_Examples"}, {"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": "2011-06-27", "Modification_Version": "2.0", "Modification_ReleaseDate": "2011-06-27", "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": "2014-06-23", "Modification_Version": "2.7", "Modification_ReleaseDate": "2014-06-23", "Modification_Comment": "updated Description, Other_Notes, 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-11-08", "Modification_Version": "3.0", "Modification_ReleaseDate": "2017-11-08", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2018-03-27", "Modification_Version": "3.1", "Modification_ReleaseDate": "2018-03-27", "Modification_Comment": "updated Relationships"}, {"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, Type"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-08-20", "Modification_Version": "4.2", "Modification_ReleaseDate": "2020-08-20", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2022-10-13", "Modification_Version": "4.9", "Modification_ReleaseDate": "2022-10-13", "Modification_Comment": "updated Applicable_Platforms, Demonstrative_Examples, Description, Name, 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 Relationships"}], "Contribution": {"@Type": "Content", "Contribution_Name": "Drew Buttner", "Contribution_Organization": "MITRE", "Contribution_Date": "2022-08-15", "Contribution_Comment": "Suggested name change and other modifications, including a new demonstrative example."}, "Previous_Entry_Name": [{"@Date": "2008-04-11", "#text": "Failure to Account for Default Case in Switch"}, {"@Date": "2009-05-27", "#text": "Failure to Use Default Case in Switch"}, {"@Date": "2022-10-13", "#text": "Missing Default Case in Switch Statement"}]}}
