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

Andrey Karpov
Articles: 356



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.

Рисунок 3

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
{
public:
  enum CursorState {
    CursorShowing,
    CursorHidden,
    CursorSuppressed
  };
  ....
}

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++, and C# code

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


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++, and C#

goto PVS-Studio;