Once again the PVS-Studio analyzer has proved to be more attentive than a person

Andrey Karpov
Articles: 565

Investigating warnings of the PVS-Studio analyzer when checking various open source projects, we see for ourselves again and again how useful this tool can be. The code analyzer is incredibly attentive and never gets tired. It indicates errors that elude even during careful code reviewing. Let's look at another such case.


The last time I wrote a similar note, exploring the source code of the StarEngine: 2D Game Engine. This time the analyzer showed its superiority over me during the check of the framework Qt.

Last time we checked the Qt framework in 2014. It's been a while since then, the project has changed and many new diagnostics have appeared in the PVS-Studio analyzer. It means that it is reasonably possible to write another article which I did.

When writing interesting examples of errors, I ran across such a code:

QWindowsCursor::CursorState QWindowsCursor::cursorState()
  enum { cursorShowing = 0x1, cursorSuppressed = 0x2 };
  CURSORINFO cursorInfo;
  cursorInfo.cbSize = sizeof(CURSORINFO);
  if (GetCursorInfo(&cursorInfo)) {
    if (cursorInfo.flags & CursorShowing)   // <= V616

The analyzer issued the following warning for this code:

V616 CWE-480 The 'CursorShowing' named constant with the value of 0 is used in the bitwise operation. qwindowscursor.cpp 669

An unstable version of PVS-Studio was used for the check, so my faith in the analyzer wavered. "Uh, we broke something in handling mechanisms of unnamed enumerations", I sighed and wrote that case to the bugtracker as an error, leading to false alarm.

I was absolutely sure that the analyzer was to blame. Moreover, just a few lines above said that the constant CursorShowing was equal to 1.

In doing so, I tried to be careful! I looked through the code several times to make sure that the analyzer was wrong. I placed that code fragment and the appropriate message as a bug in our bugtracker.

I made a careful review of that little piece of code and still goofed up. The analyzer was right there, not a person.

When performing a detailed analysis it turned out that a named cursorShowing constant was declared, and in the condition, the CursorShowing constant was used. The only difference was in the first letter! In one place it was lower case and in the other - capital.

Why was the code compiled? Because the constant CursorShowing also existed. Here is its declaration:

class QWindowsCursor : public QPlatformCursor
  enum CursorState {

As you can see, the constant CursorShowing is equal to 0. Therefore, the PVS-Studio analyzer was absolutely right, indicating that the condition (cursorInfo.flags & CursorShowing) was meaningless. The condition is always false.

The analyzer found a great typo. Like static code analysis! :)

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

Bugs Found

Checked Projects
Collected Errors
14 526
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →