Versions Compared

Key

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

...

In this example, the strlen() function is used to limit the number of times the function string_loop() will iterate. The conditional statement inside the loop is activated when the current character in the string is the NULL terminator. However, because strlen() returns the number of characters that precede the NULL terminator, the conditional statement never evaluates true.

Code Block
bgColor#FFCCCC

int string_loop(char *str) {
    size_t i;
    for (i=0; i < strlen(str); i++) {
        /* ... */
	if (str[i] == '\0')
	    /* This code is never reached */
    }
    return 0;
}

Compliant Solution 2

In this case, removing Removing the dead code requires us to adjust the terminating condition of the loop so that every element in str is processed. This can be done by not subtracting 1 from the result of strlen()depends on the intent of the programmer. Assuming the intent is to flag and process the last character before the NULL terminator, the conditional is adjusted to correctly determine if the last character has been reached.

Code Block
bgColor#ccccff
int string_loop(char *str) {
    size_t i;
    for (i=0; i < strlen(str); i++) {
        /* Process str... */
	if (str[i+1] == '\0') 
	    /* This code is now reached */
    }
    return 0;
}

Risk Assessment

...