1145
}
return result;
}
Example 3:
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 Language: Python (Bad)
result = process_data(data)
match result:
case 0:
print("Properly handle zero case.")
case 1:
print("Properly handle one case.")
# program execution continues...
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.
Example Language: Python (Good)
result = process_data(data)
match result:
case 0:
print("Properly handle zero case.")
case 1:
print("Properly handle one case.")
case _:
print("Properly handle unexpected condition.")
# program execution continues...
Example 4:
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 Language: JavaScript (Bad)
let step = input1 + input2;
switch(step) {
case 1:
alert("Process step 1.");
break;
case 2:
alert("Process step 2.");
break;
case 3:
alert("Process step 3.");
break;
}
// program execution continues...
The recommended approach is to add a default case that captures any unexpected result
conditions and properly handles them.
CWE Version 4.14
CWE-478: Missing Default Case in Multiple Condition Expression
CWE-478: Missing Default Case in Multiple Condition Expression