...
MSC37-C-EX1: According to the C Standard, 5.1.2.2.3, paragraph 1 [ISO/IEC 9899:2011], "Reaching the }
that terminates the main function returns a value of 0." As a result, it is permissible for control to reach the end of the main()
function without executing a return statement.
MSC37-C-EX2: It is permissible for a control path to not return a value if that code path is never taken and a function marked _Noreturn
is called as part of that code path. For example:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h>
#include <stdlib.h>
_Noreturn void unreachable(const char *msg) {
printf("Unreachable code reached: %s\n", msg);
exit(1);
}
enum E {
One,
Two,
Three
};
int f(enum E e) {
switch (e) {
case One: return 1;
case Two: return 2;
case Three: return 3;
}
unreachable("Can never get here");
} |
Risk Assessment
Using the return value from a non-void
function where control reaches the end of the function without evaluating a return
statement can lead to buffer overflow vulnerabilities as well as other unexpected program behaviors.
...