Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Clean up summary, move implementation details.

In C99, undefined behavior can result when If control reaches the end of a non-void function, and using the return value of the function call is used. This is almost always a programming error, and can lead to unexpected behaviorresults in undefined behavior. See also undefined behavior 7 of Appendix J.

This rule is related to MSC01-C. Strive for logical completeness.

...

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

Implementation Details

When this noncompliant code example is compiled with /-Wall on most versions of the GCC compiler,

...

bgColor#ccccff

...

.

...

the following warning is generated

...

...


example.c: In function ‘main’:
example.c:5: warning: control reaches end of non-void function

Compliant Solution

This compliant solution ensures that control never reaches the end of the checkpass function.

Code Block
bgColor#ccccff
int checkpass(char *password) {
  if (strcmp(password, "pass") == 0) {
    return 1;
  }
  return 0;
}
/* ... */
if (checkpass(userinput)) {
  printf("Success!\n");
}

Implementation Details

When this noncompliant code example is compiled with -Wall on most versions of the GCC compiler,

Code Block
bgColor#ccccff

#include <stdio.h>

int main(void) {
  printf("test\n");
}

the following warning is generated

Code Block
bgColor#ccccff

example.c: In function ‘main’:
example.c:5: warning: control reaches end of non-void function

Risk Assessment

Using the return value from a non-void function where control reaches the end of the function can lead to unexpected program behavior, and possibly abnormal program termination.

...