Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
0,1,2, Freddy came for Blender

0,1,2, Freddy came for Blender

26 Oct 2022
Author:

This article could have been named "How PVS-Studio prevents rash code changes, example N7". However, naming articles like that becomes boring. So today Freddy Krueger's name is in the title, and you'll find out why.

We're all people, we all make mistakes. Especially when we are in a hurry. Programming is no exception for that. There's suddenly a light-bulb moment, and you rush to write the code while the thought is still clear in your head. Developers do know how hard it is to collect all the puzzle pieces in your head after getting distracted. I have a pic for that:

Sometimes developers are in a hurry when writing code. Besides, they also may miss something — especially when the code seems simple. I think that's how the error below occurred. And it wasn't noticed during code review — the code is so simple, there's nowhere to go wrong.

One of the Blender developers made a commit shown in the first picture of this article, and PVS-Studio found an error there. So, I decided to write another note about how the static analyzer helps fix errors in code. Providing, of course, if it's used :). I'm not writing about all errors, only about interesting ones.

Incorrect code:

static void initSnapSpatial(TransInfo *t, float r_snap[3],
                            float *r_snap_precision)
{
  /* Default values. */
  r_snap[0] = r_snap[1] = 1.0f;
  r_snap[1] = 0.0f;
  *r_snap_precision = 0.1f;
  ....
}

The PVS-Studio analyzer issued a warning: V519. The 'r_snap[1]' variable is assigned values twice successively. Perhaps this is a mistake. transform.c. Check lines: 1727, 1728.

The developer decided to fill an array with default values, but was in a rush. As a result of a typo, there are two errors at once:

  • The second element of an array (r_snap[1]) is initialized by an incorrect default value. The developer had to write 1.0f there, but wrote 0.0f;
  • The third array element (r_snap[2]) remains uninitialized by default. Then, another value may be written to this element of the array. If the value is not written, the element remains uninitialized, and that leads to undefined behavior.

So, what does Freddy have to do with it? The answer is simple: 0,1,2 attract many errors. I wrote an article about that — "Zero, one, two, Freddy's coming for you".

From the lyrics of this song we can assume that Freddy's coming for Blender.

How to avoid such errors?

  • Don't rush when writing code.
  • Thoroughly review the code — even if it seems simple/uninteresting.
  • Use PVS-Studio to search for errors.

You don't need to choose only one point. All three are important! Use them all together. Thank you and I wish you bugless code.

Previous articles:

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