The state of static analysis in the GCC 12 compiler
developers.redhat.com
developers.redhat.com
A few weeks ago after some changes in my program it would complain about something and after figuring out that it was actually fine, I didn't want to rewrite that part just to spell things out for the analyzer. I went to check if they had made improvements. My distro still had only GCC 10 but 11 had been released so I compiled it from source and tried it: the warning disappeared, and I decided to leave the code as it was.
If you give it a try, make sure to do it with GCC 11 at least, it improved a lot since 10.
Sure, the power of warnings and gcc/clang's dedicated static analyzer modes aren't usually as good as commercial alternatives. The commercial ones either have fewer false positives or a larger suite of checks, or both. But the fact that (at least with clang) you can throw on a "-Wall --analyze" while you're compiling means that there's a huge level of convenience that you don't even need to setup or play with the build environment in order to get coverity/klockwork/etc working.
That PVS Studio is an interesting one. It has some very clever and (at one time) unique checks looking at similar consecutive statements and expressions and the identifier names used within in order to identify a pattern and find a deviation from the pattern.
The Coverity website seems a bit clunky but I think there's a CLI tool?
In GCC 12, compared to GCC 10/11, it got a whole lot more "aggressive", to the point where it's no longer possible to compile the kernel with warn-error because of many static analysis warnings in core code, and I also had to rearrange and annotate [to disable some warnings] a lot of code in my own projects to get things to compile.
So next time you compile some C code with GCC 10, 11 or 12, definitely try -fanalyzer to see what it says, but be warned that you might be presented with some complicated error messages which require quite a lot of time to analyse to see if they're real problems or false alarms.
Here are some genuine problems found:
https://github.com/libguestfs/libguestfs/commit/58599031f849... https://github.com/libguestfs/libguestfs/commit/8dd09a16f59c...
Here are some false alarms:
https://github.com/libguestfs/libguestfs/commit/ea04d6b878ec... https://github.com/libguestfs/libguestfs/commit/d00c36410b74... https://github.com/libguestfs/libguestfs/commit/9dddd8ec62d5... https://github.com/libguestfs/libguestfs/commit/8a4275c5686d...
Edit: In Coverity's favour, it supports many more languages than just C/C++
I'll have to try it out on some of my C/C++ projects.