CWE Rule 478
Description
Rule Description
The code does not have a default case in an expression with multiple conditions, such as a switch statement.
Polyspace Implementation
The rule checker checks for Missing case for switch condition.
Examples
Missing case for switch condition
This issue occurs
when the switch
variable can take values that are
not covered by a case
statement.
Note
Bug Finder only raises a defect if the switch variable is not full range.
If the switch
variable takes a value that is not covered by a
case
statement, your program can have unintended
behavior.
A switch-statement that makes a security decision is particularly vulnerable when all possible values are not explicitly handled. An attacker can use this situation to deviate the normal execution flow.
It is good practice to use a default
statement as a
catch-all for values that are not covered by a
case
statement. Even if the switch
variable takes an unintended value, the resulting behavior can be
anticipated.
#include <stdio.h> #include <string.h> typedef enum E { ADMIN=1, GUEST, UNKNOWN = 0 } LOGIN; static LOGIN system_access(const char *username) { LOGIN user = UNKNOWN; if ( strcmp(username, "root") == 0 ) user = ADMIN; if ( strcmp(username, "friend") == 0 ) user = GUEST; return user; } int identify_bad_user(const char * username) { int r=0; switch( system_access(username) ) //Noncompliant { case ADMIN: r = 1; break; case GUEST: r = 2; } printf("Welcome!\n"); return r; }
In this example, the enum
parameter User
can
take a value UNKNOWN
that is not covered by a case
statement.
One possible correction is to add a default condition for possible
values that are not covered by a case
statement.
#include <stdio.h> #include <string.h> typedef enum E { ADMIN=1, GUEST, UNKNOWN = 0 } LOGIN; static LOGIN system_access(const char *username) { LOGIN user = UNKNOWN; if ( strcmp(username, "root") == 0 ) user = ADMIN; if ( strcmp(username, "friend") == 0 ) user = GUEST; return user; } int identify_bad_user(const char * username) { int r=0; switch( system_access(username) ) { case ADMIN: r = 1; break; case GUEST: r = 2; break; default: printf("Invalid login credentials!\n"); } printf("Welcome!\n"); return r; }
Check Information
Category: Bad Coding Practices |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)