On the stackoverflow.com website, we frequently see questions about how to look for bugs of a certain type. We know that PVS-Studio can solve the problem. Unfortunately, we have to keep silent. Otherwise, Stack Overflow moderators may consider it as an obvious attempt to promote our product. This article describes a particular case of such a situation that makes us suffer deeply.
The following question: "Scan-Build for clang-13 not showing errors" asked by kratos from India, made me write this article. This person asks how to look for patterns of the following type:
Here's the code cited as an example:
int f1(){
int a=5;
short b=4;
bool a1=a;//maybe warn
bool b1=b;//maybe warn
if(a1&&b1)return 1;
return 0;
}
class M{
public:
virtual int GetAge(){return 0;}
};
class P:public M{
public:
virtual int GetAge(){return 1;}
P(){GetAge();}//maybe warn
~P(){GetAge();}//maybe warn
};
int main(){
return 0;
}
To search for errors, kratos tried to use the Clang 13 compiler, but this isn't working.
I don't know whether you can find such errors with Clang or not – didn't study this issue. Most likely, to find errors, you just have to specify the right flag for the compiler.
However, I am tempted to reply something as: try PVS-Studio. Although no – this is not enough to post an answer, but I was eager to add the comment as:
I can't tell you whether you can search for these errors with Clang, but the PVS-Studio static analyzer finds these errors right away: an example on the Compiler Explorer website. Try it, maybe it's gonna work :)
The first suspicious pattern triggers the analyzer in two ways at once. Therefore, we have 4 warnings in total:
And another two messages relating to the virtual functions call:
Some programmers may find this information useful. Unfortunately, no one will know about this on Stack Overflow :(.
First, the answer contains a commercial tool. Moreover, it's the first question from kratos.
It may look as if I created a virtual user on purpose, asked a question, and answered it to promote PVS-Studio in a favorable light.
So, I can't really post an answer. Stack Overflow moderators may think that it's a stupid spam trick and ban me :). Maybe we asked a question and answered it... At least we should have leveled up our virtual user asking the question... Not creative at all :).
Well, I didn't reply to the question on Stack Overflow, but I wrote this little note. Eventually, I feel relieved, and readers enjoy an unusual story from the life of the PVS-Studio team :).
Note. You may tartly say that such a comment promotes a proprietary tool. And Stack Overflow must fine/ban us for this. I don't think so. For many developers, it won't make any difference which tool to use. You can use PVS-Studio for free in various scenarios: "Ways to Get a Free PVS-Studio License".
Thanks for your attention. By the way, since we were talking about Clang, I invite you to take a look at a recent note "Detecting errors in the LLVM release 13.0.0". Wish you bugless code!
0