To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
100% code coverage by static analysis -…

100% code coverage by static analysis - is it that good?

Aug 29 2012

Many programmers think that the more error messages a static code analyzer produces, the better. It would be true if all the messages hit the bull's eye, as they say. But this is impossible: the same warnings may be considered both true and false by different programmers depending on the project type. There is also one more important and interesting thing. It may appear that a line between a false positive and a real error is very thin. Let's have a look at one of these cases.

We have got several similar comments from users concerning false positives generated by the analyzer. When studying the code fragments they sent to us, we faced the question how much necessary and reasonable it is to obtain 100% code coverage. This question had never arisen before, as we had been sure that the more code is analyzed, the better.

We have decided to refine this approach and change the code analyzer's behavior. Consider one of the code samples sent to us by users:

long adjustment = 0;
long total_weighting = 0;
... //the 'total_weighting' variable is not modified here
if( total_weighting > 0 )
{
  adjustment /= total_weighting;
}

An ambiguity occurs: the 'total_weighting' variable is ALWAYS equal to 0 and is not changed anywhere. If we check the "adjustment /= total_weighting;" line, we should generate the error message "V609 Divide by zero. Denominator 'total_weighting' == 0".

But it is obvious that this code branch will never be executed if the 'total_weighting' variable equals zero. The attempt to divide by zero won't occur.

It turns out that we shouldn't check the whole code. Since such cases appeared to be rather numerous, we have decided not to analyze those code fragments that never get control.

To implement this we employ the following mechanism. The analyzer calculates and gathers those expressions whose values are known without executing the program. For example:

int a = 1;
int b = a + 1;

We know for sure that 'b' will take value '2' here.

If some conditional operators contain logical expressions that surely take values 'true' or 'false', we make a decision accordingly whether or not we should analyze the code branches:

  • If the value is always true, only the 'then' branch will be analyzed.
  • If the value is always false, only the 'else' branch will be analyzed.

But when it is impossible to calculate the value of a logical expression in a conditional operator, both the 'then' and 'else' branches will be analyzed.

To have a better understanding of what is going on let's look at a possible continuation of the code from the previous sample:

int a = 1;
int b = a + 1;
if (b != 2)
{
  int *p = 0; *p = 1;
}
else
{
  ...
}

The 'b != 2' expression will always be false, which means that only the 'else' branch will be executed. Accordingly, no warning about null pointer dereferencing will be generated, as this error will never occur when executing the program.

If the initial conditions are unknown, the code analyzer's behavior will change. For instance, we will produce the error message for this code:


int b = rand() % 10;
if (b != 2)
{
  int *p = 0; *p = 1; //Error!
}
else
{
  ...
}

We omitted the question what for we need code branches that are never executed. Shouldn't we consider them errors? No, this code appears when using a lot of various programming methods. Here are some of them:

  • Executing different code fragments depending on the version. For example: if (Version == VERSION_1) ... else if (Version == VERSION_2) ...". It resembles preprocessor constructs #if-#endif but allows you to be sure that all the code branches can be successfully compiled.
  • Commenting out code fragments. You can know for sure that even though the code is not executed, it still can be properly compiled.
  • Code fragments programmers use for debugging purposes. While performing debugging you can enter such a code fragment and use it to do anything you need: for example, get access to certain values.
  • Different actions depending on type sizes. For example: if (sizeof(void *) > sizeof(int)).
  • Macro programming.
  • Other methods.

Now it turns clear that "more" doesn't necessarily mean "better". 100% code coverage cannot be an indicator of a quality code estimate. Having got rid of analysis for code fragments never executed, we obtain a fewer number of error messages, while the analysis quality is getting higher. An important error message has fewer chances to get lost among false positives, and the number of real errors remains the same.

Popular related articles
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept