Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
bgColor#FFCCCC
langc

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
bgColor#ccccff
langc

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
bgColor#FFCCCC
langc

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
bgColor#ccccff
langc

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
MSC07-EX1
MSC07-EX1
MSC07-EX1: In some situations, seemingly dead code may make software resilient. An example is the 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
bgColor#ccccff
langc

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
MSC07-EX2
MSC07-EX2
MSC07-EX2: It is also permissible to temporarily remove code that may be needed later. (See recommendation MSC04-C. Use comments consistently and in a readable fashion for an illustration.)

...

section

1 J
139 S
140 S

section

can

Can detect violations of this recommendation when the -Wunreachable-code flag is used.

GCC

can

Can detect violations of this recommendation when the -Wunreachable-code flag is used

section

.

Coverity Prevent

can

Can detect the specific instance where

Code

code can never be reached because of a logical contradiction or a dead

'

"default

'

" in switch statement.

Coverity Prevent

can

Can detect the instances where

Code

code block is unreachable because of the syntactic structure of the code.

Tool

Version

Checker

Description

LDRA tool suite

Include Page
LDRA_V
LDRA_V
Section

Fully

Implemented section

implemented.

Splint

Include Page
Splint_V
Splint_V

 

Section
Section
Include Page
GCC_V
GCC_V

 

Section

Klocwork

Include Page
Klocwork_V
Klocwork_V
section

LV_UNUSED.GEN VA_UNUSED.* UNREACH.*

 

Section
Include Page
Coverity_V
Coverity_V
section

DEADCODE

Section
Section
Include Page
Coverity_V
Coverity_V
section

UNREACHABLE

Section

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"

...