Versions Compared

Key

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

If control reaches the end of a non-void function, using the return value of the function call results in undefined behavior. See also undefined behavior 7 of Appendix J.

This rule is related to rule MSC01-C. Strive for logical completeness, as because both rules involve ensuring that programs properly handle all possible conditions.

...

This error is frequently diagnosed by compilers. (see See guideline MSC00-C. Compile cleanly at high warning levels.).

Compliant Solution

This compliant solution ensures that the checkpass() function always returns a value.

...

This compliant solution changes the interface of getlen() to store the result in a user-provided pointer and return an error code to indicate any error conditions. The best method for handling this type of error is specific to the application and the type of error. (see See guideline ERR00-C. Adopt and implement a consistent and comprehensive error-handling policy for more on error handling.).

Code Block
bgColor#ccccff
int getlen(int *input, size_t maxlen, int delim, size_t *result) {
  size_t i;
  for (i = 0; i < maxlen; ++i) {
    if (input[i] == delim) {
      if (result != NULL) {
        *result = i;
      }
      return 0;
    }
  }
  return -1;
}

/* ... */
size_t i;
int data[] = {1, 1, 1};
if (getlen(data, sizeof(data), 0, &i) != 0) {
  /* Handle error. */
} else {
  data[i] = userdata;
}

...