3 ms·
Do there exist any good tools for identifying undefined behavior in C or C++ code? Compiler warnings are getting better, and "sanitizers" can catch undefined b
by nkurz 10y ago
Do there exist any good tools for identifying undefined behavior in C or C++ code? Compiler warnings are getting better, and "sanitizers" can catch undefined behavior that actually happens at runtime, but I still haven't found anything that consistently catches unwanted optimizations based on compiler assumptions of "no undefined behavior".
Stack (https://css.csail.mit.edu/stack/ https://css.csail.mit.edu/stack/) is the best tool that I've found, and while it's a great start, it's extremely difficult to install, clumsy to use, doesn't appear to have been touched in years, and only catches a limited subset of constructs. Is there anything else out there? If not, is it because it's perceived as too difficult to do, or is it just not perceived as a need?
- telotortium 10y agoNot all undefined behavior can be caught at compile time. However, LLVM and GCC can detect a lot of undefined behavior at runtime with ubsan. I'm not sure how much performance penalty there is. Presumably checks are added on every signed integer arithmetic operation, pointer dereference, etc., but some checks can probably be removed by the optimizer (for example, a branch may be taken if a pointer is not null, which means the optimizer can remove all null checks in that branch as long as the pointer hasn't been modified). Edit: oops, I missed the part where you said you're aware of sanitizers.
- deleted 10y ago[deleted]
- tacostakohashi 10y agoProbably Coverity (http://www.synopsys.com/software/coverity/Pages/default.aspx http://www.synopsys.com/software/coverity/Pages/default.aspx) is the best / most complete implementation of this idea. Although it has impressive capabilities, you'll need to deal with their sales people, get a license, and invest a non-trivial amount of time learning the system, modifying your software build, etc, to get a lot out of in. Unless your code is mission-critical, it's probably more trouble than it's worth.
- Too 10y agoGCC has -fsanitize which is pretty good.