Versions Compared

Key

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

If control reaches the closing curly brace (}) of a non-void function without evaluating a return statement, using the return value of the function call is undefined behavior. (see See undefined behavior 88.).

Noncompliant Code Example

...

This error is frequently diagnosed by compilers. (see See 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 returns a status indicator to report success or failure. The best method for handling this type of error is specific to the application and the type of error. (see See ERR00-C. Adopt and implement a consistent and comprehensive error-handling policy for more on error handling.).

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

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

...