Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
Once again the PVS-Studio analyzer has …

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

Sep 19 2018
Author:

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.

0582_Once_again_the_PVS_Studio_analyzer_has_proved_to_be_more_attentive_than_a_person/image1.png

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! :)



Comments (0)

Next comments next comments
close comment form