Description Summary The code does not have a default case in a switch statement,
which might lead to complex logical errors and resultant
weaknesses.
Example 1 The following fails to 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: (Bad Code) C #define FAILED 0 #define PASSED 1 int result; ... result = security_check(data); switch (result) { case FAILED:
printf("Security check failed!\n");
exit(-1);
case PASSED:
printf("Security check passed.\n");
break;
} // program execution continues... ... Instead a default label should be used for unaccounted conditions: (Good Code) C #define FAILED 0 #define PASSED 1 int result; ... result = security_check(data); switch (result) { case FAILED:
printf("Security check failed!\n");
exit(-1);
case PASSED:
printf("Security check passed.\n");
break;
default:
printf("Unknown error (%d), exiting...\n",result);
exit(-1);
} 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Page Last Updated:
October 29, 2009
|
|
CWE is a Software Assurance strategic initiative sponsored by the National Cyber Security Division of the U.S. Department of Homeland Security. This Web site is hosted by The MITRE Corporation. Contact cwe@mitre.org for more information. |
|||
