There is an open source project COVID-19 CovidSim Model written in C ++. There is a static code analyzer PVS-Studio, which is good at finding errors. One day they met. Learn the fragility of mathematical modeling algorithms and why you need to put maximum effort into the quality of the program code.
The other day I needed to find something on GitHub, which is the beginning of this little story. While studying 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 very tiny. It has only 13,000 lines of code, not counting blank lines and comments. And there are almost no mistakes there either. But one mistake is so simple and beautiful that I cannot pass 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));
....
}
. - . . .
. , PVS-Studio : V614 [CWE-457] Uninitialized variable 'ModelValue' used. CovidSim.cpp 5412
, :
double ModelValue;
for (int k = offset; k < day; k++)
{
double prob_seroconvert = ....;
ModelValue += c * TimeSeries[k - offset].incI * prob_seroconvert;
}
: . .
, . . , . , , , , .
:
PVS-Studio! . .
, : Andrey Karpov. COVID-19 Research and Uninitialized Variable.