How to quickly check out interesting warnings given by the PVS-Studio analyzer for C and C++ code?
Once in a while, programmers who start getting acquainted with the PVS-Studio code analyzer ask me: “Is there a list of warnings that accurately indicate errors?” There is no such list because uninteresting (false) warnings in one project are very important and useful in another one. However, one can definitely start digging into the analyzer from the most exciting warnings. Let’s take a closer look at this topic.
Trouble is, as a rule, at first runs a programmer drowns in a huge number of warnings that he gets. Naturally, he wants to start reviewing the most interesting warnings in order to understand whether he should spend his time sorting out all this. Good, so here are three simple steps that will let him check out the most exciting warnings.
Disable all types of warnings except general ones (GA). A common mistake is to enable all types of warnings. Inexperienced users think that the more to enable, the better. That’s not the case. There are diagnostic sets, such as 64-bit checks and MISRA rules, that should only be used when one clearly knows what are they and how to work with them. For example, enabling MISRA diagnostics for an ordinary application program, you will drown in tens, thousands or hundreds of thousands of warnings such as:
- V2506. MISRA. A function should have a single point of exit at the end.
- V2507. MISRA. The body of a loop\conditional statement should be enclosed in braces.
- V2523. MISRA. All integer constants of unsigned type should have ‘u’ or ‘U’ suffix.
Most MISRA warnings indicate not errors, but code smells. Naturally, a programmer begins to ask questions. How do you find something interesting in the pile of all these warnings? What numbers should he watch? These are the wrong questions. You just need to disable the MISRA set. This is the standard for writing quality code for embedded devices. The point of the standard is to make the code extremely simple and understandable. Don’t try to apply it where it’s inappropriate.
Note. Yes, MISRA has rules designed to identify real bugs. Example: V2538 — The value of uninitialized variable should not be used. But don’t be afraid to disable the MISRA standard. You’re not going to lose anything. The real errors will still be found as part of the General Diagnostics (GA). For example, an uninitialized variable will be found by the V614 diagnostic.
Any static analyzer issues false positives at the first runs and requires some configuring. Nothing can be done about it, but it’s not as scary as it may seem. Even a simple quick setting allows you to remove most false positives and start viewing a quite relevant report. I won’t talk more about it, as I have written about it many times, for example, in this article: “Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10–15% of False Positives”.
Spend a little time disabling obviously irrelevant warnings and fighting against false positives related to macros. Generally speaking, macros are the main reason of false positives, as a warning appears in all cases when a poorly implemented macro is used. To suppress warnings in macros, you can write comments of a special type next to their declaration. The more of comments format is covered in the documentation.
Yes, the initial setting will take a little time, but will drastically improve the perception of the report by eliminating the distracting noise. Take some time to do it. If there are any difficulties or questions, we are always ready to help and tell you how to set up the analyzer in the best way. Feel free to write and ask us questions.
Start viewing warnings from Level 1. Only after it watch 2 and 3. Warning levels are nothing more than the veracity of a warning. Warnings of the Level 1 are more likely to indicate an actual error than warnings of the Level 2.
You can say, when you choose to “watch Level 1,” you press the “watch the most interesting errors” button.
In more detail, the classification of PVS-Studio warnings by levels is described in the article “The way static analyzers fight against false positives, and why they do it”.
So why isn’t there a list?
However, the idea of having a list of the most useful warnings may still seem reasonable. Let me show you in a practical example that the usefulness of a diagnostic is relative and depends on the project.
Let’s consider the V550 warning. The warning detects a potential error related to the fact that in order to compare numbers with a floating-point the operators == or != are used.
Most of the developers I’ve talked to, think that this diagnostic is useless and they disable it because all its triggerings for their project are false. That’s why this diagnostic has low level of certainty and relates to the Level 3.
Indeed, in most applications, float/double types are used in very simple algorithms. Often the comparison with the constant is used solely to check if a certain value is set by default, or whether it has changed. In this case, the exact check is quite appropriate. I’ll explain it with pseudo-code.
float value = 1.0f;
value = GetUserValue();
if (value == 1.0f)
Here the comparison (value of 1.0f) is correct and safe.
Does this mean that the V550 diagnostic is uninteresting? No. It all depends on the project. Let me quote a snippet from the article “How We Tried Static Analysis on Our X-Ray Endovascular Surgery Training Simulator Project”, written by our user.
So, what our static analyzer pays attention to here:
V550 An odd precise comparison: t != 0. It’s probably better to use a comparison with defined precision: fabs(A — B) > Epsilon. objectextractpart.cpp 3401
D3DXVECTOR3 N = VectorMultiplication(
VectorMultiplication(V-VP, VN), VN);
float t = Qsqrt(Scalar(N, N));
V = V - N * DistPointToSurface(V, VP, N);
Errors of such type repeat quite often in this library. I can’t say it came as a surprise to me. Previously, I’ve met incorrect handling of numbers with a floating point in this project. However, there were no resources to systematically verify the sources. As a result of the check, it became clear that it was necessary to give the developer something to broaden his horizons in terms of working with floating point numbers. He’s been linked to a couple of good articles. We’ll see how things turn out. It is difficult to say for sure whether this error causes real disruptions in the program. The current solution exposes a number of requirements for the original polygonal mesh of arteries, which simulates the spread of X-ray contrast matter. If the requirements are not met, the program may fall, or the work is clearly incorrect. Some of these requirements are obtained analytically, and some — empirically. It is possible that this empirical bunch of the requirements is growing just because of incorrect handling of numbers with a floating point. It should be noted that not all found cases of using precise comparison of numbers with a floating point were an error.
As you can see, what is not interesting in some projects is of interest in others. This makes it impossible to create a list of the “most interesting” ones.
Note. You can also set the level of warnings using settings. For example, if you think that the V550 diagnostic deserves close attention, you can move it from Level 3 to the 1 one. This type of settings is described in the documentation (see “How to Set Your Level for Specific Diagnostics”).
Now you know how to start studying analyzer warnings by looking at the most interesting ones. And don’t forget to look into the documentation to get a detailed description of warnings. Sometimes it happens that behind a nondescript, at first glance, warning lies hell. An example of such diagnostics: V597, V1026. Thank you for your attention.