5 ms·
The, to me, obvious thing to do with code with undefined behaviour is to emit an error and refuse to compile it. Programmers should never rely on undefined beha
by tormeh 11y ago
The, to me, obvious thing to do with code with undefined behaviour is to emit an error and refuse to compile it. Programmers should never rely on undefined behaviour.
Then again, I think languages shouldn't have undefined behaviour and that programmers who use languages with undefined behaviour deserve what they get.
- caf 11y agoIn the most interesting cases, it can't reasonably be detected at compile-time anyway. See also here: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- daurnimator 11y agoIt's usually only at runtime rather than compile time that such situations arise. At runtime we have the clang and gcc undefined behaviour sanitizers, but they aren't free in terms of performance.
- scott_s 11y agoThe problem, as Regehr (the author of this submission) has pointed out many times before, is more subtle than that. Because certain behavior is considered undefined, compilers are allowed to assume the code it is compiling is well defined, and optimize accordingly. That can cause simple bugs to have mysterious effects. For example: a->thing = 42; if (a == NULL) { return; } Obviously that code is wrong; I should check for NULL before using a. But because I dereferenced a, the compiler can assume a must not be NULL, and just removes the null check as dead code. This scenario has caused problems in the kernel, and made bad bugs even worse.
- tamana 11y agoBut the compiler could just as well emit a warning "useless NULL comparison" instead of blithely assuming that the programmer intentionally wrote a book expression. That wouldn't handle every case of UB, but would handle many.
- superuser2 11y agoA warning on removed dead code isn't helpful because dead come is legitimately removed all the time. No one would ever heed it.
- slavik81 11y agoIf you ignored dead code produced by macros and limited the check to function-scope, I think you'd mostly eliminate false positives.