New diagnostic functions implemented in PVS-Studio 3.60

Andrey Karpov
Articles: 374



1. Deprecated functions

The V303 diagnostic message.

In the Win64 API, there are some functions which are present for the compatibility purpose but might cause errors in 64-bit programs. A typical example is the SetWindowLong function.

LONG SetWindowLong(HWND hWnd, int nIndex, LONG dwNewLong);

The SetWindowLong function changes attributes of the defined window. The function also sets a 32-bit value of the LONG type at the defined shift in an additional space of memory about the window. It is often a pointer which is saved. It is safe in Win32 systems since the size of the LONG type coincides with the pointer's size. But when recompiling the code for a Win64 system, a call of this function might cause an error. Let's consider a code sample taken from a real application:

SetWindowLong(window, 0, (LONG)this);

The code is correct from the viewpoint of the Win32 system but it may cause an error in the 64-bit version of the program. Whether an error will occur or not depends upon what memory section the object referred to by the "this" pointer is created in. If the object will be created outside the first 4 Gbytes of the address space, it will lead to an unexpected behavior of the program or its crash. What is unpleasant about this error is that it might occur rarely or after a long time during program execution when objects will be created outside the first 4 Gbytes due to memory allocation.

To correct the code, you should use a new extended version of the function SetWindowLongPtr whose third parameter has the LONG_PTR type.

Beginning with the 3.60 version, the Viva64 analyzer included into PVS-Studio allows you to quickly find functions that require programmer's attention when developing 64-bit programs. To such functions, the following ones refer: SetWindowLong, GetWindowLong, SetClassLong, GetClassLong, GetFileSize and some other functions.

The following diagnostic message is intended to detect these functions: "V303: The function is deprecated in the Win64 system. It is safer to use the NewFOO function". Instead of NewFOO, analyzer writes the name of the recommended function, for example, "GetFileSizeEx".

2. Buffer overflow

The V320 diagnostic message.

Unfortunately, the errors of buffer overflow or its incomplete processing are rather frequent type of defects in programs written in the C and C++ languages. But we are interested at the moment in those errors that occur when porting applications to a 64-bit system. Such errors occur most often due to misprints or inaccuracy of the programmer. Consider an example:

STRUCT_1 Abcd;
STRUCT_2 Qwer;
memset(&Abcd, 0, sizeof(Abcd));
memset(&Qwer, 0, sizeof(Abcd));

In this code, the two structures are zeroed. When filling the second structure, the size of the first structure is used. While the sizes of the first and second structures coincide in the 32-bit code, the program works correctly. In the 64-bit program, the structures' sizes might differ and it will lead to a crash.

In some cases, the Viva64 analyzer allows to detect such errors and warn you by displaying the message " V320: A call of the FOO function will lead to a buffer overflow or underflow in a 64-bit system". Instead of FOO, analyzer writes the names of such functions as memset, memcpy, memmove, etc.

In some cases, the V320 warning will let you detect an error that is present both in the 64-bit and 32-bit versions and was not found earlier. Below is an example of an error found in the code of a real application while testing Viva64:

struct MD5Context
{
  unsigned int buf[4];
  unsigned int bits[2];
  unsigned char in[64];
};
...
MD5Context *ctx;
...
memset(ctx, 0, sizeof(ctx)); //V320

The error lies in using the "sizeof(ctx)" expression instead of "sizeof(*ctx)". It means that only a small part of the structure will be zeroed both in the 32-bit and 64-bit versions of the application.

3. Memsize-types in structures

The V122 diagnostic message.

This rule is rather specific and in 99% gives a false alarm. That is why it is disabled by default.

The V122 diagnostic message is generated for all the memsize-types (size_t, intptr_t, INT_PTR, DWORD_PTR, pointers, etc.) present in structures or classes. Presence of a pointer in a structure does not necessarily imply an error. However, it means that the structure's size changes and sometimes users would like to view a list of all such structures.

Consider an example where the "V122. Memsize type is used in the struct/class" message will be displayed:

struct Header
{
  unsigned m_version;
  size_t m_bodyLen; //V122
};

4. Users' wishes

The V2001 and V2002 diagnostic messages.

For the first time we implemented diagnostic rules in PVS-Studio that do not refer to detecting errors in 64-bit or parallel applications. These are general-purpose diagnostic messages which were created on the request of our customers. We implemented wishes of our current and potential customers in the PVS-Studio before as well, but they all concerned 64 bits or parallelism.

We think that it is the beginning of a new stage of cooperation with our users. We begin not only to ship and modify PVS-Studio but also to implement absolutely new types of diagnostic capabilities our users are interested in.

The V2001 and V2002 rules are rather specific and most likely will not be interesting among many programmers. However, we hope that you will notice that we are open to cooperation and will contact us and share your wishes. We are ready to develop specialized extensions for PVS-Studio and even separate solutions in the sphere of static code analysis. To learn more, please contact us by the e-mail support@viva64.com or through the feedback form.

P.S. Do not forget to try the "What is it?" function.



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