...
This recommendation is related to guideline MSC12-C. Detect and remove code that has no effect.
...
This noncompliant code example demonstrates how dead code can be introduced into a program [Fortify 2006]. The second conditional statement, if (s)
, will never evaluate true because it requires that s
not be assigned NULL
, and the only path where s
can be assigned a non-NULL
value ends with a return
statement.
Code Block | ||||
---|---|---|---|---|
| ||||
int func(int condition) {
char *s = NULL;
if (condition) {
s = (char *)malloc(10);
if (s == NULL) {
/* Handle Error */
}
/* Process s */
return 0;
}
/* ... */
if (s) {
/* This code is never reached */
}
return 0;
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
int func(int condition) {
char *s = NULL;
if (condition) {
s = (char *)malloc(10);
if (s == NULL) {
/* Handle error */
}
/* Process s */
}
/* ... */
if (s) {
/* This code is now reachable */
}
return 0;
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
int string_loop(char *str) {
size_t i;
size_t len = strlen(str);
for (i=0; i < len; i++) {
/* ... */
if (str[i] == '\0')
/* This code is never reached */
}
return 0;
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
int string_loop(char *str) {
size_t i;
size_t len = strlen(str);
for (i=0; i < len; i++) {
/* ... */
if (str[i+1] == '\0')
/* This code is now reached */
}
return 0;
}
|
...
Anchor | ||||
---|---|---|---|---|
|
default
label in a switch
statement whose controlling expression has an enumerated type and that specifies labels for all enumerations of the type. (See recommendation MSC01-C. Strive for logical completeness.) Since Because valid values of an enumerated type include all those of its underlying integer type, unless enumeration constants have been constants are provided for all those values, the default
label is appropriate and necessary.Code Block | ||||
---|---|---|---|---|
| ||||
typedef enum { Red, Green, Blue } Color;
const char* f(Color c) {
switch (c) {
case Red: return "Red";
case Green: return "Green";
case Blue: return "Blue";
default: return "Unknown color"; /* not dead code */
}
}
void g() {
Color unknown = (Color)123;
puts(f(unknown));
}
|
Anchor | ||||
---|---|---|---|---|
|
...
Tool | Version | Checker | Description | section||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Section | 1 J | Fully Implemented sectionimplemented. | ||||||||||
Splint |
|
| Section | Can detect violations of this recommendation when the | |||||||||
Section | GCC |
|
| Section | Can detect violations of this recommendation when the . | ||||||||
| LV_UNUSED.GEN VA_UNUSED.* UNREACH.* |
| |||||||||||
Section | |
| DEADCODE | Section | Can detect the specific instance where Codecode can never be reached because of a logical contradiction or a dead '"default '" in | ||||||||
Section | |
| UNREACHABLE | Section | Can detect the instances where Codecode block is unreachable because of the syntactic structure of the code. |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
ISO/IEC TR 24772 "BRS Leveraging human experience," "BVQ Unspecified Functionalityfunctionality," and "XYQ Dead and Deactivated Codedeactivated code"
MISRA Rule 2.4
MITRE CWE: CWE-561, "Dead Codecode"
...
Sources
[Fortify 2006] Code Quality, "Dead Codecode"
...