To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (an 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.

>
>
>
COVID-19 research and uninitialized var…

COVID-19 research and uninitialized variable

Feb 05 2021
Author:

There is an open project COVID-19 CovidSim Model, written in C++. There is also a PVS-Studio static code analyzer that detects errors very well. One day they met. Embrace the fragility of mathematical modeling algorithms and why you need to make every effort to enhance the code quality.

0796_covid_sim/image1.png

This little story begins with my ordinary search on GitHub. While looking through the search results, I accidentally came across the COVID-19 CovidSim Model project. Without thinking twice, I decided to check it using the PVS-Studio analyzer.

The project turned out to be tiny. It contains only 13,000 lines of code, not counting empty lines and comments. And there are almost no errors there either. But one mistake is so simple and beautiful that I couldn't pass it by!

void CalcLikelihood(int run, std::string const& DataFile,
                    std::string const& OutFileBase)
{
  ....
  double m = Data[row][col]; // numerator
  double N = Data[row][col + 1]; // denominator
  double ModelValue;
  // loop over all days of infection up to day of sample
  for (int k = offset; k < day; k++)
  {
    // add P1 to P2 to prevent degeneracy
    double prob_seroconvert = P.SeroConvMaxSens *
      (1.0 - 0.5 * ((exp(-((double)(_I64(day) - k)) * P.SeroConvP1) + 1.0) *
      exp(-((double)(_I64(day) - k)) * P.SeroConvP2)));
    ModelValue += c * TimeSeries[k - offset].incI * prob_seroconvert;
  }
  ModelValue += c * TimeSeries[day - offset].S * (1.0 - P.SeroConvSpec);
  ModelValue /= ((double)P.PopSize);
  // subtract saturated likelihood
  LL += m * log((ModelValue + 1e-20) / (m / N + 1e-20)) +
        (N - m) * log((1.0 - ModelValue + 1e-20) / (1.0 - m / N + 1e-20));
  ....
}

Serious scientific code. Something is calculated. Formulas. Everything looks smart and detailed.

But all these calculations shattered into pieces by human inattention. It's good that the PVS-Studio code analyzer can come to the rescue and point out the bug: V614 [CWE-457] Uninitialized variable 'modelValue' used. CovidSim.cpp 5412

Indeed, let's take a closer look at it:

double ModelValue;
for (int k = offset; k < day; k++)
{
  double prob_seroconvert = ....;
  ModelValue += c * TimeSeries[k - offset].incI * prob_seroconvert;
}

We are facing a simple and at the same time terrible error: an uninitialized variable. This algorithm can calculate anything.

Well, that's it. There is nothing to explain here. It only remains to remind again that developers of scientific libraries and scientific applications should make additional efforts to ensure the code quality. Crash of an ordinary application is likely to cost much less than the use of incorrect results for scientific, medical, and other calculations.

This is not our first article on this topic:

Use the PVS-Studio static code analyzer! When errors are timely detected you can expect enormous benefits. Thanks for your attention!

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…
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…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
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…
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…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
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…
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…
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…
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