Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
Once again the PVS-Studio analyzer has …

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

19 Sep 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.

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

Popular related articles

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam