PVS-Studio 6.26 Released

Andrey Karpov
Articles: 374



Normally we don't write notes about a release of the new version of PVS-Studio. However, this new release included many interesting improvements related to analysis of C and C++ code, about which we'd like to tell our users.

Рисунок 1

Java is Coming Soon

Frankly speaking, the latest and most exciting upgrades in PVS-Studio are still hidden. I mean support of the Java programming language by the analyzer. We haven't released a public PVS-Studio beta-version for Java yet, but it will be available very soon. If someone wants to take part in its testing, you can write to our support (choose: I want the analyzer for Java).

New Diagnostics for C and C++

In the new version we slightly got carried away and added immediately 15 general-purpose diagnostics for C and C++ (V1021-V1035). In a minor release, we've never added as many diagnostics at once. More details on each of the diagnostics can be found in the documentation. In my opinion, the most interesting ones among the new diagnostics are:

  • V1026. The variable is incremented in the loop. Undefined behavior will occur in case of signed integer overflow.
  • V1033. Variable is declared as auto in C. Its default type is int.

The V1026 diagnostic is created based on the discussion (RU) at the linux.org.ru forum. A programmer complained about GCC 8 compiler bug, but it turned out, that the reason of it was incorrect code, resulting in undefined behavior. Let's consider this case.

Note. In the original discussion, the variable s is of the type const char *s. In doing so, on the target platform the char type is unsigned. So for clarity, I immediately wrote in the example that a pointer type is const unsigned char *.

int foo(const unsigned char *s)
{
  int r = 0;
  while(*s) {
    r += ((r * 20891 + *s *200) | *s ^ 4 | *s ^ 3) ^ (r >> 1);
    s++;
  }
  return r & 0x7fffffff;
}

The compiler does not generate code for the bitwise AND (&) operator. That's why the function returns negative values, although according to the idea of a programmer, this mustn't happen.

A developer believes that this is a bug in the compiler. Nevertheless, it is the author of the code who is wrong. The function does not work correctly due to the fact that undefined behavior occurs in it.

The compiler follows that in the r variable a certain sum is calculated. Overflow of the r variable must not happen. Otherwise, it is undefined behavior, which doesn't have to be considered or taken into account by a compiler. So, the compiler thinks that since the value of r variable after ending the loop cannot be negative, then r & 0x7fffffff operation is not needed to reset the sigh bit and the compiler just returns the value of r variable from the function.

It is the V1026 diagnostic that is designed to detect such errors. To fix the code, it is enough to read hash using an unsigned variable for this. Correct code variant:

int foo(const unsigned char *s)
{
  unsigned r = 0;
  while(*s) {
    r += ((r * 20891 + *s *200) | *s ^ 4 | *s ^ 3) ^ (r >> 1);
    s++;
  }
  return (int)(r & 0x7fffffff);
}

Now let's look at another diagnostic V1033. It is interesting because of the fact that the reason of possible errors was the new keyword auto introduced in C++11. However, not the C++11 innovation is to blame, but some psychological nuances :). Now let me explain. Take a look at this code:

float d = 3.14f;
int i = 1;
auto sum = d + i;

Did you notice an error in it? Think for a while. Here's a picture so you don't read the text immediately.

Рисунок 2

Guess what might be wrong? If not, here is some more interesting information. The sum variable will be equal to 4 instead of 4.14. Why?

Рисунок 3

Now a reader is going to say that it was an unfair riddle! The thing is that this is not C++, but C.

It so happens a project contains C++ and oldy worldy C. The programmer gets used to using auto in C++ and accidentally can use this keyword in C. But there, it means something quite different:

auto

Defines a local variable as having a local lifetime. Keyword auto uses the following syntax:

[auto] data-definition;

As the local lifetime is the default for local variables, auto keyword is extremely rarely used.

It turns out that the sum variable has the int type, which is why its value will be equal to 4.

Рисунок 4

Even though the error may seem exotic, actually, in the project, which uses a mixture of C and C++ files, it can be make very easily. Accordingly, when analyzing C files, PVS-Studio warns about such suspicious constructions.

Other New Features

The ability to check projects for a Waf build system is added.

We continue to develop the analyzer towards embedded systems. In this version we added support for checking projects aimed to be built GNU Arm Embedded Toolchain, Arm Embedded GCC compiler.

When analyzing projects for Visual C++ compiler (cl.exe, vcxproj projects for Visual Studio/Standalone), in the analyzer report the case is preserved in paths to checked files. Refinement looks simpler than it is in reality. When preprocessing files, the cl.exe compiler spoils the case in file names. So we have to restore them back in the analyzer.

We also added the ability to use pvsconfig files with CLMonitor/Standalone on Windows.

Incremental analysis mode is added for pvs-studio-analzyer/CMake module. PVS-Studio CMake module can now be used on Windows for projects built with the Visual C++ compiler (cl.exe).

Incremental analysis support for .NET Core/.NET Standard Visual Studio projects is now available.

Additional Links



Use PVS-Studio to search for bugs in C, C++, C# and Java

We offer you to check your project code with PVS-Studio. Just one bug found in the project will show you the benefits of the static code analysis methodology better than a dozen of the articles.

goto PVS-Studio;

Andrey Karpov
Articles: 374


Bugs Found

Checked Projects
355
Collected Errors
13 303