Pull to refresh
385.09
Rating
PVS-Studio
Static Code Analysis for C, C++, C# and Java

How we sympathize with a question on StackOverflow but keep silent

PVS-Studio corporate blog Programming *

How we sympathize with a question on StackOverflow but keep silent
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, StackOverflow 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:


  • writing integer values other than 0 and 1 to a bool type variable;
  • virtual functions call in the constructor and destructor.

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:


  • 6:1: note: V547 The 'A = a' expression is equivalent to the 'A = true' expression.
  • 6:1: warning: V786 It is odd that value 'a' is assigned to the 'a1' variable. The value range of 'a1' variable: [0, 1].
  • 7:1: note: V547 The 'A = b' expression is equivalent to the 'A = true' expression.
  • 7:1: warning: V786 It is odd that value 'b' is assigned to the 'b1' variable. The value range of 'b1' variable: [0, 1].

And another two messages relating to the virtual functions call:


  • 18:1: error: V1053 Calling the 'GetAge' virtual function in the constructor may lead to unexpected result at runtime.
  • 19:1: error: V1053 Calling the 'GetAge' virtual function in the destructor may lead to unexpected result at runtime.

Some programmers may find this information useful. Unfortunately, no one will know about this on StackOverflow :(.


First, the answer contains a commercial tool. Moreover, it's the first question from kratos.


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. StackOverflow 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 StackOverflow, 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 StackOverflow 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!

Tags:
Hubs:
Total votes 2: ↑1 and ↓1 0
Views 434
Comments Leave a comment

Information

Founded
2008
Website
pvs-studio.com
Employees
31–50 employees
Registered