4 ms·
> Probably not something you’d encounter in practice it's actually probably the most common footgun you'll encounter in practice: non-void functions with no re
by mathisfun123 8d ago
> Probably not something you’d encounter in practice
it's actually probably the most common footgun you'll encounter in practice: non-void functions with no return statements just keep executing past their end. ask me how i know.
compile with -Wreturn-type if you want to avoid such things...
- kouosi 8d ago> compile with -Wreturn-type if you want to avoid such things... Isn't -Wreturn-type enabled by default in both gcc and clang atleast for c++?
- mathisfun123 8d agoprobably - i guess then i meant -Werror=return-type
- mitxela 7d agoIn C. In C++ it's an error to not return, as it should be.
- mathisfun123 7d ago.... The blog post literally demonstrates that it is not. Feel free to repro on your own machine.
- mitxela 7d agoThe main function in the blog post returns at the end of all control paths. Firstly because main has an implicit "return 0" at the end and secondly because no control paths end. It doesn't jump to unreachable because it lacks a return - it jumps there because that's how this implementation has chosen to compile UB, despite the presence of all necessary returns.
- drdexebtjl 8d agoC on the other hand puts an implicit `return 0` at the end, but only on the main function for some reason. Very weird.
- mpyne 8d agoC++ also special-cases the `main` function. Probably because `main` is the interface to the OS so it gets special language treatment.