﻿# PVS\-Studio 6\.26 Released

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\.

![0585_PVS_Studio_6_26/image1.png](https://import.viva64.com/docx/blog/0585_PVS_Studio_6_26/image1.png)

## 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](https://pvs-studio.com/en/about-feedback/) \(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](https://pvs-studio.com/en/docs/warnings/v1010/)\. 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](https://pvs-studio.com/en/docs/warnings/v1026/) diagnostic is created based on the [discussion \[RU\]](https://www.linux.org.ru/forum/development/14422428) 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 \*_\.

```cpp
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: 

```cpp
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](https://pvs-studio.com/en/docs/warnings/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:

```cpp
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\.

![0585_PVS_Studio_6_26/image2.png](https://import.viva64.com/docx/blog/0585_PVS_Studio_6_26/image2.png)

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?

![0585_PVS_Studio_6_26/image3.png](https://import.viva64.com/docx/blog/0585_PVS_Studio_6_26/image3.png)

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:_

```cpp
[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\.

![0585_PVS_Studio_6_26/image4.png](https://import.viva64.com/docx/blog/0585_PVS_Studio_6_26/image4.png)

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](https://github.com/viva64/pvs-studio-waf-examples) for a [Waf](https://en.wikipedia.org/wiki/Waf) build system is added\. 

We continue to [develop](https://pvs-studio.com/en/blog/posts/cpp/0561/) 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

1. PVS\-Studio\. [PVS\-Studio Release History](https://pvs-studio.com/en/docs/manual/0010/)
1. Andrey Karpov\. [Undefined behavior is closer than you think](https://pvs-studio.com/en/blog/posts/cpp/0374/)\.
1. Will Dietz, Peng Li, John Regehr, and Vikram Adve\. [Understanding Integer Overflow in C/C\+\+](https://pvs-studio.com/en/blog/posts/cpp/0374/)\.
1. Egor Bredikhin\. [Development of a new static analyzer: PVS\-Studio Java PVS\-Studio Java](https://pvs-studio.com/en/blog/posts/java/0572/)\.